【Python】系统模块os下的路径操作

os模块:系统模块
  • Python编程时,经常和文件、目录打交道。os模块包含普通的系统操作功能,与具体的平台无关
常用命令
  • os.name
    输出字符串指示正在使用的平台。如果是Windows则为'nt',对Linux/Unix用户则为'posix'。
  • os.getcwd()
    函数得到当前工作目录,即当前Python脚本工作的目录路径。
os.getcwd()
# 'C:\\Users\\Administrator'
  • os.listdir()
    返回制定目录下的所有文件和目录名。
  • os.chdit()
    切换到当前路径。
os.chdir('C:\\Users\\Administrator\\Desktop')
  • os.remove()
    删除一个文件。
  • os.system()
    运行shell命令。os.system('cmd') os.system('python')
  • os.path.split()
    函数返回一个路径的目录名和文件名。
os.path.split('C:\\Users\\Administrator\\Desktop\\lianxi\\test2.txt')
# ('C:\\Users\\Administrator\\Desktop\\lianxi', 'test2.txt')
  • os.path.exists()
    用来检验给出的路径是否真的存在。
os.path.exists('C:\\Users\\Administrator\\Desktop\\lianxi\\test2.txt') # False
os.path.exists('C:\\Users\\Administrator\\Desktop\\lianxi') # True

你可能感兴趣的:(【Python】系统模块os下的路径操作)