Python文件或目录操作的常用函数

◆ os.listdir(path)

Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.


◆ os.getcwd() method displays the current working directory


◆ os.chdir() method to change the current directory


◆ other file and directory operation functions:
os.rename(current_file_name, new_file_name)
os.remove(file_name)
os.mkdir("newdir")

os.rmdir('dirname')


◆ 内置变量__file__可以获得文件名(有可能是相对路径名),进一步调用 os.path.realpath(__file__)可以获得该文件的路径完整路径名(并且已经展开链接文件)。


◆ os.path.dirname(filename)可以获得文件所在的目录名。


◆ os.path.basename(filename)可以获得完整路径名最后的文件名,如果是一个目录,则返回""。


◆ os.path.join(path1, path2)可以将多个路径参数连接,这也是一个常用的函数。


◆ os.curdir和os.pardir是两个常量字符串:"."和"..",分别表示当前目录和上一级目录的字符串。


◆ 获取当前程序所在目录的完整路径

_curpath=os.path.normpath( os.path.join( os.getcwd(), os.path.dirname(__file__) )  )



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值