2022年 11月 7日

python获取当前目录路径和文件

一、获取当前路径

      1、使用sys.argv[0]

  1. import sys
  2. print sys.argv[0]
  3. #输出
  4. #本地路径

      2、os模块

  1. import os
  2. print os.getcwd() #获取当前工作目录路径
  3. print os.path.abspath('.') #获取当前文件目录路径
  4. print os.path.abspath('test.txt') #获取当前目录文件下的文件目录路径
  5. print os.path.abspath('..') #获取当前文件目录的父目录 !注意是父目录路径
  6. print os.path.abspath(os.curdir) #获取当前文件目录路径

    3、改变当前目录

         1) 使用: os.chdir(path)。

  1. import os, sys
  2. path = "/tmp"
  3. # 查看当前工作目录
  4. retval = os.getcwd()
  5. print "当前工作目录为 %s" % retval
  6. # 修改当前工作目录
  7. os.chdir( path )
  8. # 查看修改后的工作目录
  9. retval = os.getcwd()
  10. print "目录修改成功 %s" % retval
  11. #执行以上程序输出结果为:
  12. 当前工作目录为 /www
  13. 目录修改成功 /tmp

   

         2) 使用: os.path 路径相关方法的使用:

  1. import os
  2. print( os.path.basename('/root/runoob.txt') ) # 返回文件名
  3. print( os.path.dirname('/root/runoob.txt') ) # 返回目录路径
  4. print( os.path.split('/root/runoob.txt') ) # 分割文件名与路径
  5. print( os.path.join('root','test','runoob.txt') ) # 将目录和文件名合成一个路径
  6. #输出结果
  7. runoob.txt
  8. /root
  9. ('/root', 'runoob.txt')
  10. root/test/runoob.txt

         3) 使用:os.path 文件相关方法的使用:

  1. import os
  2. import time
  3. file='/root/runoob.txt' # 文件路径
  4. print( os.path.getatime(file) ) # 输出最近访问时间
  5. print( os.path.getctime(file) ) # 输出文件创建时间
  6. print( os.path.getmtime(file) ) # 输出最近修改时间
  7. print( time.gmtime(os.path.getmtime(file)) ) # 以struct_time形式输出最近修改时间
  8. print( os.path.getsize(file) ) # 输出文件大小(字节为单位)
  9. print( os.path.abspath(file) ) # 输出绝对路径
  10. print( os.path.normpath(file) ) # 规范path字符串形式
  11. #输出结果
  12. 1539052805.5735736
  13. 1539052805.5775735
  14. 1539052805.5735736
  15. time.struct_time(tm_year=2018, tm_mon=10, tm_mday=9, tm_hour=2, tm_min=40, tm_sec=5, tm_wday=1, tm_yday=282, tm_isdst=0)
  16. 7
  17. /root/runoob.txt
  18. /root/runoob.txt

   

4、组合路径返回

no.1

  1. #方式一,直接用“+”:
  2. >>> print("D:\\home" + "\\report\\" + "config.ini")
  3. D:\\home\\report\\config.ini
  4. #方式二,用join拼接:
  5. >>> print os.path.join('D:\home','report','config.ini'
  6. D:\home\report\config.ini
  7. >>> print os.path.join('D:', 'file_one', 'file_two')
  8. E:\file_one\file_two
  9. >>> print os.path.join('\home', '\home\file_one\', '\home\file_one\file_two\')
  10. \home\file_one\file_two\

no.2

  1. '''
  2. os.getcwd() 是返回当前工作路径
  3. 例如:file.py文件位于:D:\\Test\\testcase\\file.py,在file.py文件中使用os.getcwd()会获取到D:\\Test路径。如果在C:\\CTest\\ctestcase\\file2.py中进行调用file.py文件时会获取到C:\\CTest路径。
  4. PS:当前工作路径 working directory 就是脚本运行/调用/执行的地方,而不是脚本本身的地方。
  5. '''
  6. import os
  7. root = os.getcwd() #获得当前路径 /home/dir1
  8. print root
  9. #输出
  10. #/home/dir1
  11. name = "file1" #定义文件名字
  12. print(os.path.join(root, name)) #合并路径名字和文件名字,并打印
  13. #输出
  14. #/home/dir1/file1

二、获得当前目录下所有文件

      1. os.walk() 是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。

  1. 概述
  2. os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下。
  3. os.walk() 方法是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。
  4. 在Unix,Windows中有效。
  5. 语法
  6. os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
  7. 参数
  8. top -- 是你所要遍历的目录的地址, 返回的是一个三元组(root,dirs,files)。
  9. root -- 所指的是当前正在遍历的这个文件夹的本身的地址
  10. dirs -- 是一个 list ,内容是该文件夹中所有的目录的名字(不包括子目录)
  11. files -- 同样是 list , 内容是该文件夹中所有的文件(不包括子目录)
  12. topdown --可选,为 True,则优先遍历 top 目录,否则优先遍历 top 的子目录(默认为开启)。如果 topdown 参数为 True,walk 会遍历top文件夹,与top 文件夹中每一个子目录。
  13. onerror -- 可选,需要一个 callable 对象,当 walk 需要异常时,会调用。
  14. followlinks -- 可选,如果为 True,则会遍历目录下的快捷方式(linux 下是软连接 symbolic link )实际所指的目录(默认关闭),如果为 False,则优先遍历 top 的子目录。
  15. 返回值
  16. 返回生成器。

    2.os.walk() 实例:

  1. import os
  2. for root, dirs, files in os.walk(".", topdown=False):
  3. for name in files:
  4. print(os.path.join(root, name))
  5. for name in dirs:
  6. print(os.path.join(root, name))
  7. #输出结果:
  8. ./.bash_logout
  9. ./amrood.tar.gz
  10. ./.emacs
  11. ./httpd.conf
  12. ./www.tar.gz
  13. ./mysql.tar.gz
  14. ./test.py
  15. ./.bashrc
  16. ./.bash_history
  17. ./.bash_profile
  18. ./tmp
  19. ./tmp/test.py