Python3 - os模块和shutil模块常用方法及实例

os 模块

  1. os.getcwd() 函数
    getcwd = get current working directory得到当前工作目录,即当前Python脚本工作的目录路径
    >>> os.getcwd() '/Users/Documents'
  2. os.name
    字符串指示你正在使用的平台。比如对于Windows,它是'nt',而对于Linux/Unix用户,它是 'posix'
    >>> os.name 'posix'
  3. os.listdir(path)
    返回给定目录path下所有内容列表(也包含所有隐藏的文件夹和文件)

     >>> os.listdir('/Users/virgil/Documents/GitHub/PySpider')
     ['.DS_Store', '.git', '.vscode', 'README.md', 'test.py']
  4. os.remove(path)
    用来删除一个文件,path必须是文件名,如果是文件夹会报错PermissionError: [Errno 1] Operation not permitted:
    >>> os.remove('/Users/Documents/GitHub/testtttt/README.MD')
  5. os.removedirs(path)
    递归删除目录,只能删除目录下没有内容的文件夹,否则报错OSError: [Errno 66] Directory not empty:
    >>> os.removedirs('/Users/Documents/GitHub/testtttt')
  6. os.curdir 返回当前目录 ('.')
    >>> os.curdir '.'
  7. os.mkdir(path) 创建一个目录
    >>> os.mkdir('testaaaaaaaa') #在当前工作目录os.getcwd()下创建文件夹'testaaaaaaaa' >>> os.mkdir('testaaaaaaaa/test/test1') #不存在test目录,报错FileNotFoundError: [Errno 2] No such file or directory: 'testaaaaaaaa/test/test1'
  8. os.makedirs(path) 递归的创建目录
    >>> os.makedirs('test/test1/test2') #新建test目录,在test下创建test1目录,在test1下创建test2目录
  9. os.chdir(dirname) 改变工作目录到dirname
    >>> os.chdir('/Users/Documents/GitHub') >>> os.getcwd() '/Users/Documents/GitHub'
  10. os.system(command) 函数用来运行shell命令

  11. os.path.split(path) 函数返回一个路径的目录名和文件名

    >>> os.path.split('/Users/Documents/GitHub/testtttt')
    ('/Users/Documents/GitHub', 'testtttt')
    >>> os.path.split('/Users/Documents/GitHub/testtttt/README.MD')
    ('/Users/Documents/GitHub/testtttt', 'README.MD')
  12. os.path.isfile(path) 和os.path.isdir(path)函数分别检验给出的路径是一个文件还是目录

  13. os.path.exists(path) 函数用来检验给出的路径是否真地存在

  14. os.curdir 返回当前目录 ('.')

15.os.chdir(dirname) 改变工作目录到dirname
>>> os.getcwd() '/Users/Documents' >>> os.chdir('/Users/virgil/Documents/Apple/GitHub') >>> os.getcwd() '/Users/Documents/GitHub'

  1. os.path.getsize(name) 获得文件大小,如果name是目录返回0L
  2. os.path.abspath(name) 获得绝对路径
  3. os.path.normpath(path) 规范path字符串形式
  4. os.path.splitext() 分离文件名与扩展名

    >>> os.path.split('/Users/virgil/Documents/Apple/GitHub/testtttt')
    ('/Users/Documents/GitHub', 'testtttt')
    >>> os.path.split('/Users/Documents/GitHub/testtttt/README.MD')
    ('/Users/Documents/GitHub/testtttt', 'README.MD')
  5. os.path.join(path,name) 连接目录与文件名或目录
  6. os.path.basename(path) 返回文件名
  7. os.path.dirname(path) 返回文件路径

shutil模块:It is a utility module which can be used to accomplish tasks, such as: copying, moving, or removing directory trees
参考:https://www.pythonforbeginners.com/os/python-the-shutil-module

  1. shutil.copy(src,dest)
    将目标文件拷贝到目标路径,目标路径必须存在。
    如果源文件已经存在目标路径,直接覆盖。
    拷贝到目标路径后,文件的状态(连接时间和更新时间)都会被更新,文件名、文件内容和文件的权限都不变。
    import shutil import os source = os.listdir("/tmp/") destination = "/tmp/newfolder/" for files in source: if files.endswith(".txt"): shutil.copy(files,destination)

  2. shutil.copyfile(src,dest)
    拷贝文件,src和dest是带文件名的路径,如/Users/Documents/test.txt'
    源文件必须存在,拷贝的文件内容不变,可以改变目标文件名,xyz.txt是test.txt的复制,内容一样,名字改了
    import shutil shutil.copyfile('/Users/Documents/test.txt','/Users/Documents/GitHub/xyz.txt')

  3. shutil.move(src,dest)
    递归地将文件或目录(src)移动到另一个位置(dst)。目标目录应该不存在。
    如果目标是一个目录或指向一个目录的符号链接,那么src将被移动到该目录中。
    下面的例子:把文件以.txt结尾的文件移动到目标路径。
    import shutil import os source = os.listdir("/tmp/") destination = "/tmp/newfolder/" for files in source: if files.endswith(".txt"): shutil.move(files,destination)

  4. shutil.copytree(src , dest)
    递归地将src下的整个目录树复制到dest。
    src必须存在,dest必须不存在。
    import shutil import os os.getcwd() #得到当前工作目录 SOURCE = "test" #文件夹test BACKUP = "samples-bak" # create a backup directory shutil.copytree(SOURCE, BACKUP) print(os.listdir(BACKUP)) #以list形式列出backup目录下所有文件

  5. shutil.rmtree(path)
    递归地删除目录和目录下的所有内容
    例子:删除文件系统中目录为“three”及其下面的所有内容
    import shutil shutil.rmtree('one/two/three')

猜你喜欢

转载自www.cnblogs.com/AppleZhang/p/9337574.html