一、获取当前路径
1、使用sys.argv[0]
- import sys
- print sys.argv[0]
- #输出
- #本地路径
2、os模块
- import os
- print os.getcwd() #获取当前工作目录路径
- print os.path.abspath('.') #获取当前文件目录路径
- print os.path.abspath('test.txt') #获取当前目录文件下的文件目录路径
- print os.path.abspath('..') #获取当前文件目录的父目录 !注意是父目录路径
- print os.path.abspath(os.curdir) #获取当前文件目录路径
3、改变当前目录
1) 使用: os.chdir(path)。
- import os, sys
-
- path = "/tmp"
-
- # 查看当前工作目录
- retval = os.getcwd()
- print "当前工作目录为 %s" % retval
-
- # 修改当前工作目录
- os.chdir( path )
-
- # 查看修改后的工作目录
- retval = os.getcwd()
-
- print "目录修改成功 %s" % retval
-
-
- #执行以上程序输出结果为:
-
- 当前工作目录为 /www
- 目录修改成功 /tmp
2) 使用: os.path 路径相关方法的使用:
- import os
-
- print( os.path.basename('/root/runoob.txt') ) # 返回文件名
- print( os.path.dirname('/root/runoob.txt') ) # 返回目录路径
- print( os.path.split('/root/runoob.txt') ) # 分割文件名与路径
- print( os.path.join('root','test','runoob.txt') ) # 将目录和文件名合成一个路径
-
- #输出结果
- runoob.txt
- /root
- ('/root', 'runoob.txt')
- root/test/runoob.txt
3) 使用:os.path 文件相关方法的使用:
- import os
- import time
-
- file='/root/runoob.txt' # 文件路径
-
- print( os.path.getatime(file) ) # 输出最近访问时间
- print( os.path.getctime(file) ) # 输出文件创建时间
- print( os.path.getmtime(file) ) # 输出最近修改时间
- print( time.gmtime(os.path.getmtime(file)) ) # 以struct_time形式输出最近修改时间
- print( os.path.getsize(file) ) # 输出文件大小(字节为单位)
- print( os.path.abspath(file) ) # 输出绝对路径
- print( os.path.normpath(file) ) # 规范path字符串形式
-
- #输出结果
- 1539052805.5735736
- 1539052805.5775735
- 1539052805.5735736
- 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)
- 7
- /root/runoob.txt
- /root/runoob.txt
4、组合路径返回
no.1
- #方式一,直接用“+”:
- >>> print("D:\\home" + "\\report\\" + "config.ini")
- D:\\home\\report\\config.ini
-
- #方式二,用join拼接:
- >>> print os.path.join('D:\home','report','config.ini')
- D:\home\report\config.ini
-
- >>> print os.path.join('D:', 'file_one', 'file_two')
- E:\file_one\file_two
-
- >>> print os.path.join('\home', '\home\file_one\', '\home\file_one\file_two\')
- \home\file_one\file_two\
no.2
- '''
- os.getcwd() 是返回当前工作路径
- 例如:file.py文件位于:D:\\Test\\testcase\\file.py,在file.py文件中使用os.getcwd()会获取到D:\\Test路径。如果在C:\\CTest\\ctestcase\\file2.py中进行调用file.py文件时会获取到C:\\CTest路径。
- PS:当前工作路径 working directory 就是脚本运行/调用/执行的地方,而不是脚本本身的地方。
- '''
-
-
- import os
- root = os.getcwd() #获得当前路径 /home/dir1
- print root
- #输出
- #/home/dir1
-
- name = "file1" #定义文件名字
- print(os.path.join(root, name)) #合并路径名字和文件名字,并打印
- #输出
- #/home/dir1/file1
二、获得当前目录下所有文件
1. os.walk() 是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。
- 概述
- os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下。
- os.walk() 方法是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。
- 在Unix,Windows中有效。
-
- 语法
- os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
-
- 参数
- top -- 是你所要遍历的目录的地址, 返回的是一个三元组(root,dirs,files)。
- root -- 所指的是当前正在遍历的这个文件夹的本身的地址
- dirs -- 是一个 list ,内容是该文件夹中所有的目录的名字(不包括子目录)
- files -- 同样是 list , 内容是该文件夹中所有的文件(不包括子目录)
- topdown --可选,为 True,则优先遍历 top 目录,否则优先遍历 top 的子目录(默认为开启)。如果 topdown 参数为 True,walk 会遍历top文件夹,与top 文件夹中每一个子目录。
- onerror -- 可选,需要一个 callable 对象,当 walk 需要异常时,会调用。
- followlinks -- 可选,如果为 True,则会遍历目录下的快捷方式(linux 下是软连接 symbolic link )实际所指的目录(默认关闭),如果为 False,则优先遍历 top 的子目录。
-
- 返回值
- 返回生成器。
2.os.walk() 实例:
- import os
- for root, dirs, files in os.walk(".", topdown=False):
- for name in files:
- print(os.path.join(root, name))
- for name in dirs:
- print(os.path.join(root, name))
-
- #输出结果:
- ./.bash_logout
- ./amrood.tar.gz
- ./.emacs
- ./httpd.conf
- ./www.tar.gz
- ./mysql.tar.gz
- ./test.py
- ./.bashrc
- ./.bash_history
- ./.bash_profile
- ./tmp
- ./tmp/test.py