国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 文件與目錄操作

2019-11-25 18:45:18
字體:
來源:轉載
供稿:網友
1)os.path
1.1 os.path.isabs(path) 是否是絕對路徑
1.2 os.path.isfile(path)
1.3 os.path.isdir(path)
1.4 os.path.islink(path) 是否是鏈接;但如果系統不支持鏈接,返回False
1.5 os.path.ismount(path) 是否為驅動器;但是很不幸的是在python 3.0中這是個不能運行的函數。
原函數如下:


# 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




其錯誤之處是顯而易見的。不知道這個函數為什么這么寫,在windows平臺,可以如下完成該功能
def ismount(path):
p = splitdrive(path)[1]
if len(p) > 0:
return(False)
else:
return(True)


其他平臺沒有對應的機器,不知道具體情形。
1.6 os.path.abspath(path) 返回絕對路徑
1.7 os.path.dirname(path)
1.8 os.path.exists(path)
1.9 os.path.lexists(path) 和exists函數一樣
1.10os.path.getsize(path)
1.11os.path.getctime(path) 返回浮點數的系統時間,在類Unix系統上是文件最近更改的時間,
在Windows上是文件或目錄的創建時間
1.12os.path.getmtime(path) 文件或目錄最后更改的時間
1.13os.path.getatime(path) 文件或目錄最后存取的時間
1.14os.path.samefile(path1,path2) 如果2個路徑指向同樣的文件或目錄,返回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
簡單使用
import file
input for line in fileinput.input():
process(line)


2.1 fileinput.input([files[, inplace[, backup[,mode[,openhook]]]]])
創建一個fileinput的實例,如果files為空,則指向控制臺獲得輸入;如果file為'-',同樣轉向控制臺獲得輸入。
默認情況,文件以text mode打開,如果需要其他格式,則需要指定。
2.2 fileinput.filename() #只有當讀入第一行之后,該值才被賦值
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
可以使用簡單的方法匹配某個目錄下的所有子目錄或文件,用法也很簡單。
3.1 glob.glob(regression) 返回一個列表
3.2 glob.iglob(regression) 返回一個遍歷器
這個模塊簡單好用,強力推薦。


4)linecache
看名字就知道了,屬于緩存類的
4.1 linecache.getline(filename,lineno[, module_globals]) #獲得filename的第lineno行
4.2 linecache.clearcache()
4.3 linecache.checkcache([filename]) #檢查更新


5)shutil 重點推薦的襖,好東西,支持文件集合的復制和刪除操作
5.1 shutil.copyfileobj(fsrc, fdst[, length])
5.2 shutil.copyfile(src, dst) #上面2個都是文件的復制
5.3 shutil.copymode(src, dst) #除了復制內容,還會復制其他的一些信息,例如作者
5.4 shutil.copystat(src, dst) #除了復制內容,還會復制存取時間的信息
5.5 shutil.copy(src, dst) #復制文件到dst,當dst為目錄時,復制到子目錄
5.6 shutil.copy2(src, dst) #相當于先copy再copystat
5.7 shutil.copytree(src, dst[, symlinks=False[, ingore=None]]) #復制文件夾樹,注意,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)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金湖县| 益阳市| 民县| 砀山县| 灌南县| 浙江省| 石首市| 靖边县| 韶山市| 武宣县| 新闻| 巴楚县| 巍山| 大理市| 北流市| 纳雍县| 扬州市| 阿克苏市| 白银市| 会理县| 大埔区| 赞皇县| 珠海市| 班玛县| 响水县| 南昌市| 湘乡市| 新乡市| 吴川市| 高密市| 建阳市| 永城市| 永春县| 满洲里市| 沧州市| 大新县| 广水市| 宝兴县| 塔城市| 德化县| 陈巴尔虎旗|