python 文件與目錄操作
更新時(shí)間:2008年12月24日 23:16:36 作者:
可以使用簡(jiǎn)單的方法匹配某個(gè)目錄下的所有子目錄或文件,用法也很簡(jiǎn)單。
1)os.path
1.1 os.path.isabs(path) 是否是絕對(duì)路徑
1.2 os.path.isfile(path)
1.3 os.path.isdir(path)
1.4 os.path.islink(path) 是否是鏈接;但如果系統(tǒng)不支持鏈接,返回False
1.5 os.path.ismount(path) 是否為驅(qū)動(dòng)器;但是很不幸的是在python 3.0中這是個(gè)不能運(yùn)行的函數(shù)。
原函數(shù)如下:
# Is a path a mount point? Either a root (with or without drive letter)
# or an UNC path with at most a / or \ after the mount point.
def ismount(path):
"""Test whether a path is a mount point (defined as root of drive)"""
unc, rest = splitunc(path)
seps = _get_bothseps(p)
if unc:
return rest in p[:0] + seps
p = splitdrive(path)[1]
return len(p) == 1 and p[0] in seps
其錯(cuò)誤之處是顯而易見(jiàn)的。不知道這個(gè)函數(shù)為什么這么寫(xiě),在windows平臺(tái),可以如下完成該功能
def ismount(path):
p = splitdrive(path)[1]
if len(p) > 0:
return(False)
else:
return(True)
其他平臺(tái)沒(méi)有對(duì)應(yīng)的機(jī)器,不知道具體情形。
1.6 os.path.abspath(path) 返回絕對(duì)路徑
1.7 os.path.dirname(path)
1.8 os.path.exists(path)
1.9 os.path.lexists(path) 和exists函數(shù)一樣
1.10os.path.getsize(path)
1.11os.path.getctime(path) 返回浮點(diǎn)數(shù)的系統(tǒng)時(shí)間,在類(lèi)Unix系統(tǒng)上是文件最近更改的時(shí)間,
在Windows上是文件或目錄的創(chuàng)建時(shí)間
1.12os.path.getmtime(path) 文件或目錄最后更改的時(shí)間
1.13os.path.getatime(path) 文件或目錄最后存取的時(shí)間
1.14os.path.samefile(path1,path2) 如果2個(gè)路徑指向同樣的文件或目錄,返回True(Windows上不可用)
1.15os.path.split(path) 分割路徑,如果path是目錄,返回[parentName, dirName];
如果path是文件,返回[dirName, fileName]
1.16os.path.splitext(path) 分割路徑,如果path是目錄,返回[parentName, ''];
如果path是文件,返回[dirName+fileName, 文件后綴]
2)fileinput
簡(jiǎn)單使用
import file
input for line in fileinput.input():
process(line)
2.1 fileinput.input([files[, inplace[, backup[,mode[,openhook]]]]])
創(chuàng)建一個(gè)fileinput的實(shí)例,如果files為空,則指向控制臺(tái)獲得輸入;如果file為'-',同樣轉(zhuǎn)向控制臺(tái)獲得輸入。
默認(rèn)情況,文件以text mode打開(kāi),如果需要其他格式,則需要指定。
2.2 fileinput.filename() #只有當(dāng)讀入第一行之后,該值才被賦值
2.3 fileinput.fileno()
2.4 fileinput.lineno()
2.5 fileinput.filelineno()
2.6 fileinput.isfirstline()
2.7 fileinput.isstdin()
2.8 fileinput.nextfile()
2.9 fileinput.close()
3)glob
可以使用簡(jiǎn)單的方法匹配某個(gè)目錄下的所有子目錄或文件,用法也很簡(jiǎn)單。
3.1 glob.glob(regression) 返回一個(gè)列表
3.2 glob.iglob(regression) 返回一個(gè)遍歷器
這個(gè)模塊簡(jiǎn)單好用,強(qiáng)力推薦。
4)linecache
看名字就知道了,屬于緩存類(lèi)的
4.1 linecache.getline(filename,lineno[, module_globals]) #獲得filename的第lineno行
4.2 linecache.clearcache()
4.3 linecache.checkcache([filename]) #檢查更新
5)shutil 重點(diǎn)推薦的襖,好東西,支持文件集合的復(fù)制和刪除操作
5.1 shutil.copyfileobj(fsrc, fdst[, length])
5.2 shutil.copyfile(src, dst) #上面2個(gè)都是文件的復(fù)制
5.3 shutil.copymode(src, dst) #除了復(fù)制內(nèi)容,還會(huì)復(fù)制其他的一些信息,例如作者
5.4 shutil.copystat(src, dst) #除了復(fù)制內(nèi)容,還會(huì)復(fù)制存取時(shí)間的信息
5.5 shutil.copy(src, dst) #復(fù)制文件到dst,當(dāng)dst為目錄時(shí),復(fù)制到子目錄
5.6 shutil.copy2(src, dst) #相當(dāng)于先copy再copystat
5.7 shutil.copytree(src, dst[, symlinks=False[, ingore=None]]) #復(fù)制文件夾樹(shù),注意,dst文件夾必須是不存在的
5.8 shutil.rmtree(path[, ignore_erros[, onerror]])
5.9 shutil.move(src,dst)
def copytree(src, dst, symlinks=False):
names = os.listdir(src)
os.makedirs(dst)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
else:
copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error) as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error as err:
errors.extend(err.args[0])
try:
copystat(src, dst)
except WindowsError:
# can't copy file access times on Windows
pass
except OSError as why:
errors.extend((src, dst, str(why)))
if errors:
raise Error(errors)
1.1 os.path.isabs(path) 是否是絕對(duì)路徑
1.2 os.path.isfile(path)
1.3 os.path.isdir(path)
1.4 os.path.islink(path) 是否是鏈接;但如果系統(tǒng)不支持鏈接,返回False
1.5 os.path.ismount(path) 是否為驅(qū)動(dòng)器;但是很不幸的是在python 3.0中這是個(gè)不能運(yùn)行的函數(shù)。
原函數(shù)如下:
# Is a path a mount point? Either a root (with or without drive letter)
# or an UNC path with at most a / or \ after the mount point.
def ismount(path):
"""Test whether a path is a mount point (defined as root of drive)"""
unc, rest = splitunc(path)
seps = _get_bothseps(p)
if unc:
return rest in p[:0] + seps
p = splitdrive(path)[1]
return len(p) == 1 and p[0] in seps
其錯(cuò)誤之處是顯而易見(jiàn)的。不知道這個(gè)函數(shù)為什么這么寫(xiě),在windows平臺(tái),可以如下完成該功能
def ismount(path):
p = splitdrive(path)[1]
if len(p) > 0:
return(False)
else:
return(True)
其他平臺(tái)沒(méi)有對(duì)應(yīng)的機(jī)器,不知道具體情形。
1.6 os.path.abspath(path) 返回絕對(duì)路徑
1.7 os.path.dirname(path)
1.8 os.path.exists(path)
1.9 os.path.lexists(path) 和exists函數(shù)一樣
1.10os.path.getsize(path)
1.11os.path.getctime(path) 返回浮點(diǎn)數(shù)的系統(tǒng)時(shí)間,在類(lèi)Unix系統(tǒng)上是文件最近更改的時(shí)間,
在Windows上是文件或目錄的創(chuàng)建時(shí)間
1.12os.path.getmtime(path) 文件或目錄最后更改的時(shí)間
1.13os.path.getatime(path) 文件或目錄最后存取的時(shí)間
1.14os.path.samefile(path1,path2) 如果2個(gè)路徑指向同樣的文件或目錄,返回True(Windows上不可用)
1.15os.path.split(path) 分割路徑,如果path是目錄,返回[parentName, dirName];
如果path是文件,返回[dirName, fileName]
1.16os.path.splitext(path) 分割路徑,如果path是目錄,返回[parentName, ''];
如果path是文件,返回[dirName+fileName, 文件后綴]
2)fileinput
簡(jiǎn)單使用
import file
input for line in fileinput.input():
process(line)
2.1 fileinput.input([files[, inplace[, backup[,mode[,openhook]]]]])
創(chuàng)建一個(gè)fileinput的實(shí)例,如果files為空,則指向控制臺(tái)獲得輸入;如果file為'-',同樣轉(zhuǎn)向控制臺(tái)獲得輸入。
默認(rèn)情況,文件以text mode打開(kāi),如果需要其他格式,則需要指定。
2.2 fileinput.filename() #只有當(dāng)讀入第一行之后,該值才被賦值
2.3 fileinput.fileno()
2.4 fileinput.lineno()
2.5 fileinput.filelineno()
2.6 fileinput.isfirstline()
2.7 fileinput.isstdin()
2.8 fileinput.nextfile()
2.9 fileinput.close()
3)glob
可以使用簡(jiǎn)單的方法匹配某個(gè)目錄下的所有子目錄或文件,用法也很簡(jiǎn)單。
3.1 glob.glob(regression) 返回一個(gè)列表
3.2 glob.iglob(regression) 返回一個(gè)遍歷器
這個(gè)模塊簡(jiǎn)單好用,強(qiáng)力推薦。
4)linecache
看名字就知道了,屬于緩存類(lèi)的
4.1 linecache.getline(filename,lineno[, module_globals]) #獲得filename的第lineno行
4.2 linecache.clearcache()
4.3 linecache.checkcache([filename]) #檢查更新
5)shutil 重點(diǎn)推薦的襖,好東西,支持文件集合的復(fù)制和刪除操作
5.1 shutil.copyfileobj(fsrc, fdst[, length])
5.2 shutil.copyfile(src, dst) #上面2個(gè)都是文件的復(fù)制
5.3 shutil.copymode(src, dst) #除了復(fù)制內(nèi)容,還會(huì)復(fù)制其他的一些信息,例如作者
5.4 shutil.copystat(src, dst) #除了復(fù)制內(nèi)容,還會(huì)復(fù)制存取時(shí)間的信息
5.5 shutil.copy(src, dst) #復(fù)制文件到dst,當(dāng)dst為目錄時(shí),復(fù)制到子目錄
5.6 shutil.copy2(src, dst) #相當(dāng)于先copy再copystat
5.7 shutil.copytree(src, dst[, symlinks=False[, ingore=None]]) #復(fù)制文件夾樹(shù),注意,dst文件夾必須是不存在的
5.8 shutil.rmtree(path[, ignore_erros[, onerror]])
5.9 shutil.move(src,dst)
復(fù)制代碼 代碼如下:
def copytree(src, dst, symlinks=False):
names = os.listdir(src)
os.makedirs(dst)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
else:
copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error) as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error as err:
errors.extend(err.args[0])
try:
copystat(src, dst)
except WindowsError:
# can't copy file access times on Windows
pass
except OSError as why:
errors.extend((src, dst, str(why)))
if errors:
raise Error(errors)
相關(guān)文章
vim自動(dòng)補(bǔ)全插件YouCompleteMe(YCM)安裝過(guò)程解析
這篇文章主要介紹了vim自動(dòng)補(bǔ)全插件YouCompleteMe(YCM)安裝過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
快速實(shí)現(xiàn)基于Python的微信聊天機(jī)器人示例代碼
本篇文章主要介紹了快速實(shí)現(xiàn)基于Python的微信聊天機(jī)器人示例代碼,基于itchat開(kāi)發(fā),可以用它做一個(gè)微信聊天機(jī)器人,有興趣的可以了解一下。2017-03-03
PyTorch中torch.nn.Linear實(shí)例詳解
torch.nn是包含了構(gòu)筑神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)基本元素的包,在這個(gè)包中可以找到任意的神經(jīng)網(wǎng)絡(luò)層,下面這篇文章主要給大家介紹了關(guān)于PyTorch中torch.nn.Linear的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
pytest使用@pytest.mark.parametrize()實(shí)現(xiàn)參數(shù)化的示例代碼
這篇文章主要介紹了pytest使用@pytest.mark.parametrize()實(shí)現(xiàn)參數(shù)化,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
用python寫(xiě)爬蟲(chóng)簡(jiǎn)單嗎
在本篇內(nèi)容里小編給大家整理的是關(guān)于用python寫(xiě)爬蟲(chóng)是否簡(jiǎn)單的相關(guān)內(nèi)容文章,需要的朋友們可以學(xué)習(xí)下。2020-07-07
python爬取Ajax動(dòng)態(tài)加載網(wǎng)頁(yè)過(guò)程解析
這篇文章主要介紹了python爬取Ajax動(dòng)態(tài)加載網(wǎng)頁(yè)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Pycharm自帶Git實(shí)現(xiàn)版本管理的方法步驟
這篇文章主要介紹了Pycharm自帶Git實(shí)現(xiàn)版本管理的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

