os.getcwd:得到当前工作目录,即当前python脚本工作的目录路径。
os.name:输出字符串指示正在使用的平台。如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'.
os.listdir():返回指定目录下的所有文件和目录名。和(os.getcwd配合使用)
---->>>os.listdir(os.getcwd())
$os.path.split():函数返回一个路径的目录名和文件名:
---->os.path.split('C:\\Python25\\abc.txt')------>('C:\\Python25', 'abc.txt')
$os.path.basename(path):返回文件名
>>> os.path.basename('c:\\Python\\a.txt')--->'a.txt'$os.path.join(path,name):连接目录与文件名或目录
-->> os.path.join('c:\\Python','a.txt')---->'c:\\Python\\a.txt'os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录
os.path.exists('path')函数用来检验给出的路径是否真地存在
os.path.abspath(name):获得绝对路径
os.path.splitext():分离文件名与扩展名:
--> os.path.splitext('a.txt') ('a', '.txt') 和os.path.slip()一起使用先是分出文件,然后用os.path.sliptext()分出文件名os.path.dirname(path):返回文件路径(在模块导入中经常用到) os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
对文件操作
os.remove(file):删除一个文件
os.mkdir(name):创建目录
os.rmdir(name):删除目录
os.path.getsize(name):或得文件大小,如果name是目录返回0L
os.path.isabs():判断是否为绝对路径
目录操作
os. mkdir ( "file" ) 创建目录 shutil.copyfile( "oldfile" , "newfile" ) 复制文件:oldfile和newfile都只能是文件 shutil.copy( "oldfile" , "newfile" ) oldfile只能是文件夹,newfile可以是文件,也可以是目标目录 shutil.copytree( "olddir" , "newdir" ) 复制文件夹.olddir和newdir都只能是目录,且newdir必须不存在 os.rename( "oldname" , "newname" ) 重命名文件(目录).文件或目录都是使用这条命令 shutil.move( "oldpos" , "newpos" ) 移动文件(目录) os. rmdir ( "dir" ) 只能删除空目录 shutil.rmtree( "dir" ) 空目录、有内容的目录都可以删 os.chdir( "path" ) 转换目录,换路径转载于:https://www.cnblogs.com/z18271397173/p/8508958.html