玖叶教程网

前端编程开发入门

「python课程,精心总结」os和os.path模块

import os
print(os.getcwd())#输出:C:\Users\rzhou006\PycharmProjects\pythonProject\file
lst=os.listdir('C:\\Users\\rzhou006\\PycharmProjects\\pythonProject\\file')#前面要加上反斜杠进行转义
print(lst)#输出:['file.py', 'test.txt', 'test1.txt', 'test2.txt']
import os.path
import os
#os.chdir('C:\\project\\pythonstudy')
print(os.path.abspath('file.py'))#输出绝对路径C:\Users\rzhou006\PycharmProjects\pythonProject\file\file.py和
#os.mkdir('C:\\project\\pythonstudy\\os')
print(os.path.join('C:\\project\\pythonstudy\\os','file.py'))#输出C:\project\pythonstudy\os\file.py
print(os.path.split('C:\\project\\pythonstudy\os\\file.py'))#拆分目录('C:\\project\\pythonstudy\\os', 'file.py')
print(os.path.splitext('file.py'))#拆分文件('file', '.py')
print(os.path.basename('C:\\project\\pythonstudy\\os\\file.py'))#把文件名字提取出来file.py
print(os.path.dirname('C:\\project\\pythonstudy\os\\file.py'))#把目录提取出来C:\project\pythonstudy\os
print(os.path.isdir('C:\\project\\pythonstudy\os\\file.py'))#False

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言