Created
January 31, 2019 12:32
-
-
Save 01x01/94be8586351033e75e3f767cb6a185ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 绝对路径 | |
os.pathp.abspath(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 查看文件是否存在 | |
# 一共有三种属性,分别是 存在,可读,可写,可执行 | |
# F_OK | |
# R_OK | |
# W_OK | |
# X_OK | |
os.access("tes1t.txt",os.F_OK) # 返回 True or False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 复制文件 | |
# 从源src复制到dst中去。当然前提是目标地址是具备可写权限。抛出的异常信息为IOException. 如果当前的dst已存在的话就会被覆盖掉 | |
shutil.copyfile(src, dst) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 删除文件 | |
os.remove("test.txt") # 如果参数是一个目录路径的话,会报错 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 得到目录名 | |
os.path.dirname(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 判断路径是否存在 | |
os.path.exists(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 得到文件名 | |
os.path.basename(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
os.path.isfile(path) | |
os.path.isdir(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 为什么需要路径拼接呢? 因为在 windows 用 \ 来表示路径,而 *unix 使用 / 来表示路径 | |
os.path.join(os.getcwd(),"foler") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 列出目录下的所有文件和目录 | |
os.listdir(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 打包 | |
shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]]) | |
# 打包当前目录 | |
import shutil | |
shutil.make_archive("test","zip") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 递归创建目录 | |
os.makedirs("src/test/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 移动文件 或者 重命名文件 | |
shutil.move(src, dst) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 当前绝对路径 | |
os.getcwd() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 递归移除目录 | |
shutil.rmtree( src ) # 递归删除一个目录以及目录内的所有内容 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 分离文件和拓展名 | |
os.path.splitext(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 解压缩 | |
shutil.unpack_archive(filename[, extract_dir[, format]]) | |
# 解压文件到 test 文件夹 文件夹 | |
shutil.unpack_archive("test.zip","test") | |
shutil.unpack_archive("test.zip","test") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 遍历目录 | |
''' | |
top: 给定的路径 | |
topdown: 自上而下搜索 | |
onerror: 函数,调用需要传一个参数,OSError实例,出现错误后执行 | |
followelinks: 通过软连接进行访问 | |
返回三个值: | |
dirname: 返回文件夹的相对路径 | |
dirpath: 返回的是一个集合,是当前遍历到的所有文件夹的集合 | |
filenames: 返回的也是一个集合,文件名集合 | |
''' | |
# os.walk(top, topdown=True, onerror=None, followlinks=False) | |
def walk_folder(top,extension,absolute=False,relative=False,topdown=True, onerror=None, followlinks=False): | |
files = [] | |
for dirname,dirpath,filenames in os.walk(top,topdown=topdown,onerror=onerror,followlinks=followlinks): | |
for file in filenames: | |
if os.path.splitext(file)[1] == extension: | |
if absolute and relative: | |
print('Chose absolute or relative') | |
return | |
elif absolute: | |
absolute_path = os.path.join(os.getcwd(),os.path.join(dirname,file)) | |
print(absolute_path) | |
files.append(absolute_path) | |
elif relative: | |
absolute_path = os.path.join(dirname,file) | |
print(absolute_path) | |
files.append(absolute_path) | |
else: | |
print(file) | |
files.append(file) | |
return files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment