文件操作系列之os模块(一)

291 阅读6分钟

1.文件相关的操作

1.1 文件重命名

os.rename(src,dst):对文件进行重命名, src为原来的文件名, dst为重命名后的文件名

eg:

重命名前:

image-20220508162157938.png

import os
os.rename('test_doc', 'test_bak')

重命名后:

image-20220508162330549.png

1.2 文件删除

os.remove(path):删除path所指的文件。如果path是目录,会引发OSError

删除前:

image-20220508162330549.png

os.remove('test_bak')

删除后:

image-20220508162445079.png

2.目录相关的操作

2.1 目录创建

os.mkdir(path):创建path所指的目录,如果该目录已存在,则会引发FileExistsError

创建前:

image-20220508162445079.png

os.mkdir('test_data')

创建后:

image-20220508162608670.png

2.2 目录删除

os.rmdir(path):删除path所指的目录,如果目录非空,会引发OSError

删除前:

image-20220508162608670.png

os.rmdir('test_data')

删除后:

image-20220508162445079.png

2.3 目录遍历

以D:\workspace\Python_excerise目录为例

image-20220508162837807.png

2.3.1 只遍历当前层级

os.listdir(path):列出path所指的目录下的文件和子目录
print(os.listdir('D:\workspace\Python_excerise'))

result:

['.git', '.idea', '123.py', 'data_structure_algorithms', 'python_300_examples', 'README.md']

2.3.2 递归遍历

os.walk(path):列出path所指的目录树,自顶向下遍历目录树,返回值是一个(目录路径,目录名列表,文件名列表)的元组
for root, dirs, files in os.walk('D:\workspace\Python_excerise'):
    print(f'root=={root}')
    print(f'dirs=={dirs}')
    print(f'files=={files}')

result:

root==D:\workspace\Python_excerise
dirs==['.git', '.idea', 'data_structure_algorithms', 'python_300_examples']
files==['123.py', 'README.md']
root==D:\workspace\Python_excerise.git
dirs==['hooks', 'info', 'logs', 'objects', 'refs']
files==['COMMIT_EDITMSG', 'config', 'description', 'FETCH_HEAD', 'HEAD', 'index', 'ORIG_HEAD', 'packed-refs']
root==D:\workspace\Python_excerise.git\hooks
dirs==[]
files==['applypatch-msg.sample', 'commit-msg.sample', 'fsmonitor-watchman.sample', 'post-update.sample', 'pre-applypatch.sample', 'pre-commit.sample', 'pre-merge-commit.sample', 'pre-push.sample', 'pre-rebase.sample', 'pre-receive.sample', 'prepare-commit-msg.sample', 'push-to-checkout.sample', 'update.sample']
root==D:\workspace\Python_excerise.git\info
dirs==[]
files==['exclude']
root==D:\workspace\Python_excerise.git\logs
dirs==['refs']
files==['HEAD']
root==D:\workspace\Python_excerise.git\logs\refs
dirs==['heads', 'remotes']
files==[]
root==D:\workspace\Python_excerise.git\logs\refs\heads
dirs==[]
files==['main']
root==D:\workspace\Python_excerise.git\logs\refs\remotes
dirs==['origin']
files==[]
root==D:\workspace\Python_excerise.git\logs\refs\remotes\origin
dirs==[]
files==['HEAD', 'main']
root==D:\workspace\Python_excerise.git\objects
dirs==['02', '06', '1c', '24', '26', '3e', '3f', '52', '55', '5d', '5e', '5f', '63', '66', '6a', '6c', '73', '93', '9a', '9d', 'c4', 'd5', 'e0', 'e6', 'e9', 'fb', 'info', 'pack']
files==[]
root==D:\workspace\Python_excerise.git\objects\02
dirs==[]
files==['743251b560a199890335f5d8a83d9986a47ac8']
root==D:\workspace\Python_excerise.git\objects\06
dirs==[]
files==['9e114770998a5c6ae563cc27d152386db17235']
root==D:\workspace\Python_excerise.git\objects\1c
dirs==[]
files==['a36fd8990a9f7054b3c025f2969565d8ee402c']
root==D:\workspace\Python_excerise.git\objects\24
dirs==[]
files==['8f03ce966d889e400a1b062441fcffb8a054e7']
root==D:\workspace\Python_excerise.git\objects\26
dirs==[]
files==['60bb0faf4b74b91d7607b3eb61db1685a38347', 'f22d674d98ee4af47c695a26ea099d3730816f']
root==D:\workspace\Python_excerise.git\objects\3e
dirs==[]
files==['38824a9179a97e6e8096ef5fbc1de57139ea93']
root==D:\workspace\Python_excerise.git\objects\3f
dirs==[]
files==['70d004bdde2215b86370c9e37f024e26cd0afe']
root==D:\workspace\Python_excerise.git\objects\52
dirs==[]
files==['06c5a1a52dc02b5dd37adc4251c7a597038706']
root==D:\workspace\Python_excerise.git\objects\55
dirs==[]
files==['668b2c30b7f9399d9a71c8ab6ce763af2a8e90']
root==D:\workspace\Python_excerise.git\objects\5d
dirs==[]
files==['12d12eb5e5de0fa76262f89136a123f2e3f6fd']
root==D:\workspace\Python_excerise.git\objects\5e
dirs==[]
files==['bd9645c19a4f5c52ff889a3edd5f427176f2b3']
root==D:\workspace\Python_excerise.git\objects\5f
dirs==[]
files==['9004cf448b67a0150334d94ab00977addcca89']
root==D:\workspace\Python_excerise.git\objects\63
dirs==[]
files==['8aea233cfeb34d45d0dabd38fa4efd34288372']
root==D:\workspace\Python_excerise.git\objects\66
dirs==[]
files==['faf4cff76e77c8d8f1aa10ad9c05e1b1d08e58']
root==D:\workspace\Python_excerise.git\objects\6a
dirs==[]
files==['3d2ce5f4646da8aec3028367b29ef43fcf8217']
root==D:\workspace\Python_excerise.git\objects\6c
dirs==[]
files==['614b8f010f148201eba4554c525361794bf054']
root==D:\workspace\Python_excerise.git\objects\73
dirs==[]
files==['eafa7d1983e72f67a1b1c0fa4c55317d1f4f7a']
root==D:\workspace\Python_excerise.git\objects\93
dirs==[]
files==['68352bf68dd97522124aa8547912152116bf80']
root==D:\workspace\Python_excerise.git\objects\9a
dirs==[]
files==['e172f2057679ffa843225fed3b157b3825dc27']
root==D:\workspace\Python_excerise.git\objects\9d
dirs==[]
files==['5c7fa94145e2e8d58f37b93c0a25880b108179']
root==D:\workspace\Python_excerise.git\objects\c4
dirs==[]
files==['5cc1507de67cf87947d533b22c249689cefca8']
root==D:\workspace\Python_excerise.git\objects\d5
dirs==[]
files==['805533d8789b8759c7414baf046c819ce478bb']
root==D:\workspace\Python_excerise.git\objects\e0
dirs==[]
files==['1c0a23c2bc1de6c2f9c389db5faff0af9bfc33']
root==D:\workspace\Python_excerise.git\objects\e6
dirs==[]
files==['9de29bb2d1d6434b8b29ae775ad8c2e48c5391']
root==D:\workspace\Python_excerise.git\objects\e9
dirs==[]
files==['312ed71d3d07881af48f95ab62f978aa0da6f9']
root==D:\workspace\Python_excerise.git\objects\fb
dirs==[]
files==['7efe66c7325523dc81f9f98e9fa77b0c000950']
root==D:\workspace\Python_excerise.git\objects\info
dirs==[]
files==[]
root==D:\workspace\Python_excerise.git\objects\pack
dirs==[]
files==['pack-278d92a6f4b1b62e784076b538bed8b1d7d8f85d.idx', 'pack-278d92a6f4b1b62e784076b538bed8b1d7d8f85d.pack']
root==D:\workspace\Python_excerise.git\refs
dirs==['heads', 'remotes', 'tags']
files==[]
root==D:\workspace\Python_excerise.git\refs\heads
dirs==[]
files==['main']
root==D:\workspace\Python_excerise.git\refs\remotes
dirs==['origin']
files==[]
root==D:\workspace\Python_excerise.git\refs\remotes\origin
dirs==[]
files==['HEAD', 'main']
root==D:\workspace\Python_excerise.git\refs\tags
dirs==[]
files==[]
root==D:\workspace\Python_excerise.idea
dirs==['inspectionProfiles', 'sonarlint']
files==['.gitignore', 'misc.xml', 'modules.xml', 'python_excerise.iml', 'vcs.xml', 'workspace.xml']
root==D:\workspace\Python_excerise.idea\inspectionProfiles
dirs==[]
files==['Project_Default.xml']
root==D:\workspace\Python_excerise.idea\sonarlint
dirs==['issuestore']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore
dirs==['0', '2', '4', '5', '8', '9', 'a', 'b', 'e', 'f']
files==['index.pb']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\0
dirs==['a', 'd']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\0\a
dirs==[]
files==['0a6b47649573a8d55e1d2aeea93584cb3b005797']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\0\d
dirs==[]
files==['0db1c990d27104f75043bc5e6ebad8380efd6979']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\2
dirs==['a']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\2\a
dirs==[]
files==['2a94b87f947d731f2d0baccafb1cc130a007b012']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\4
dirs==['e']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\4\e
dirs==[]
files==['4e2baa765b22d8e3edd2d6bbb1fdfbcc332f52b2']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\5
dirs==['3', '8', 'f']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\5\3
dirs==[]
files==['5312ef9744dc50fe789ed70be2a14b04ac884f13']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\5\8
dirs==[]
files==['5883fc7fcf570ca4b8c13d4558226dadcfdab995']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\5\f
dirs==[]
files==['5f4e53949f077c0466718e62d7febecb18c03937']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\8
dirs==['e']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\8\e
dirs==[]
files==['8ef445c2c92b119a1844f64e2077112b160ae488', '8ef62e570964fbe88d56efae45b33a9188f3a926']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\9
dirs==['b']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\9\b
dirs==[]
files==['9bb8e99bed7064e343ddef1ba9aa636ee18bb98b']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\a
dirs==['5']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\a\5
dirs==[]
files==['a551b43420b83f15261f4131591a8bcb39a62d43']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\b
dirs==['c']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\b\c
dirs==[]
files==['bc3e8575ac0ae62ef8717adf7ad8fa244e130c4a']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\e
dirs==['5']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\e\5
dirs==[]
files==['e52aecaf0c2817cc5ca716a2b48307a77887eaa9']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\f
dirs==['3', 'a']
files==[]
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\f\3
dirs==[]
files==['f33d521531326af17b889ff0dfbb6b9b727791c0']
root==D:\workspace\Python_excerise.idea\sonarlint\issuestore\f\a
dirs==[]
files==['faf586f32bb65feb31a11552518d7b690e368253']
root==D:\workspace\Python_excerise\data_structure_algorithms
dirs==['chapter_01']
files==['test.md', '__init__.py']
root==D:\workspace\Python_excerise\data_structure_algorithms\chapter_01
dirs==[]
files==['basic.py', 'innovate.py', 'project.py', '__init__.py']
root==D:\workspace\Python_excerise\python_300_examples
dirs==[]
files==['example_01.py', 'example_02.py', 'example_03.py', 'example_04.py', 'example_05.py', 'example_06.py', 'example_07.py', 'example_08.py', 'example_09.py', 'example_10.py', 'example_11.py', 'example_12.py', 'example_13.py', 'test.md', '__init__.py']