OS模块部分方法
os.getcwd()
os.listdir(path)
os.getenv()
os.putenv()
os.remove(path)
os.system(command='') # 执行一条shell命令
os.chdir(path)
OS模块path方法
os.path.isfile()
os.path.isdir()
os.path.exists()
os.path.getsize(filename='')
os.path.abspath(path='')
os.path.splitext(path='')
os.path.split(path='')
os.path.join(path, filename)
os.path.basename(path)
os.path.dirname(path)
例子:
返回某目录下的所有文件名
import os
def get_file(path):
if os.path.exists(path):
files = os.listdir(path)
if len(files) == 0:
print(path + " 目录为空")
else:
for file in files:
if os.path.isfile(os.path.join(path, file)):
print(os.path.basename(file))
elif os.path.isdir(os.path.join(path, file)):
get_file(file)
else:
print(path + " 路径不存在")
if __name__ == '__main__':
path = ''
get_file(path)