首页 技术 正文
技术 2022年11月15日
0 收藏 730 点赞 4,765 浏览 3377 个字
打印文件的绝对路径:
os.path.abspath(__file__)

os.path.dirname(“/root/python/test.py”)   #只取目录名
‘/root/python’

os.path.dirname(os.path.dirname(“/root/python/test.py”))   
‘/root’

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(BASE_DIR)

os模块中关于文件/目录常用的函数使用方法

函数名 使用方法
getcwd() 返回当前工作目录
chdir(path) 改变工作目录
listdir(path=’.’) 列举指定目录中的文件名(’.’表示当前目录,’..’表示上一级目录)
mkdir(path) 创建单层目录,如该目录已存在抛出异常
makedirs(path) 递归创建多层目录,如该目录已存在抛异常,注意’E:\\a\\b’和’E:\\a\\c’并不冲突
remove(path) 删除文件
rmdir(path) 删除单层目录,如该目录非空则抛出异常
removedirs(path) 递归删除目录,从子目录到父目录逐层尝试删除,遇到目录非空则抛出异常
rename(old, new) 将文件old重命名为new
system(command) 运行系统的shell命令
walk(top) 遍历top路径以下所有的子目录,返回一个三元组:(路径, [包含目录], [包含文件])
以下是支持路径操作中常用到的一些定义,支持所有平台
os.curdir 指代当前目录(’.’)
os.pardir 指代上一级目录(’..’)
os.sep 输出操作系统特定的路径分隔符(Win下为’\\’,Linux下为’/’)
os.linesep 当前平台使用的行终止符(Win下为’\r\n’,Linux下为’\n’)
os.name 指代当前使用的操作系统(包括:’posix’,  ‘nt’, ‘mac’, ‘os2’, ‘ce’, ‘java’)

 os.path模块中关于路径常用的函数使用方法

函数名 使用方法
basename(path) 去掉目录路径,单独返回文件名
dirname(path) 去掉文件名,单独返回目录路径
join(path1[, path2[, ..]]) 将path1, path2各部分组合成一个路径名
split(path) 分割文件名与路径,返回(f_path, f_name)元组。
splitext(path) 分离文件名与扩展名,返回(f_name, f_extension)元组
getsize(file) 返回文件的大小,单位是字节
getatime(file) 返回文件最近的访问时间(浮点数,可用time.gmtime()或localtime()换算)
getctime(file) 返回文件的创建时间(浮点数,可用time.gmtime()或localtime()换算)
getmtime(file) 返回文件最新的修改时间(浮点数,可用time.gmtime()或localtime()换算)
以下为函数返回 True 或 False
exists(path) 判断路径(目录或文件)是否存在
isabs(path) 判断路径是否为绝对路径
isdir(path) 判断路径是否存在且是一个目录
isfile(path) 判断路径是否存在且是一个文件
islink(path) 判断路径是否存在且是一个符号链接
ismount(path) 判断路径是否存在且是一个挂载点
samefile(path1, paht2) 判断path1和path2两个路径是否指向同一个文件

转自:https://fishc.com.cn/forum.php?mod=viewthread&tid=45512&extra=page%3D1%26filter%3Dtypeid%26typeid%3D403 

>>> os.path.split('/etc/filebeat.yml')  #将路径名和文件名分开
('/etc', 'filebeat.yml')
>>> os.path.splitext('filebeat.yml') # 将文件名和后缀名分开
('filebeat', '.yml')
>>> os.path.splitext('/etc/filebeat.yml')
('/etc/filebeat', '.yml')>>> os.path.join('/home/www','testdir') #拼接路径
'/home/www/testdir'
>>> os.path.isdir('data') # 判断是否是目录
True
>>> os.path.isfile('data') #判断是否是文件
False
>>> [ x for x in os.listdir('.') if os.path.isdir(x)] #列出当前目录下的所有目录
>>> [x for x in os.listdir() if os.path.isfile(x) and os.path.splitext(x)[1] == '.py'] #当前目录下所有后缀为.py的文件

os.walk()方法 遍历树

os.walk()方法 遍历树
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])top 要遍历的目录的地址。得到3个元组,(dirpath,dirnames,filenames)
topdown 为True(默认)时,则优先遍历top目录,自上而下遍历。 否则优先遍历top的子目录(默认为开启),自下而上。cd /root/test
# tree
.
|-- day1
| |-- base.py
| `-- day1.py
|-- day2
| |-- day2.py
| `-- day3
| `-- a.txt
`-- test.py
directories, 5 files>>> for paths,dirs,files in os.walk('/root/test'):
... print(paths,dirs,files)
...
/root/test ['day1', 'day2'] ['test.py']
/root/test/day1 [] ['day1.py', 'base.py']
/root/test/day2 ['day3'] ['day2.py']
/root/test/day2/day3 [] ['a.txt']>>> for paths,dirs,files in os.walk('/root/test'):
... print(paths)
...
/root/test
/root/test/day1
/root/test/day2
/root/test/day2/day3
>>> for paths,dirs,files in os.walk('/root/test'):
... print(dirs)
...
['day1', 'day2']
[]
['day3']
[]>>> for paths,dirs,files in os.walk('/root/test'):
... print(files)
...
['test.py']
['day1.py', 'base.py']
['day2.py']
['a.txt']>>> for paths,dirs,files in os.walk('/root/test'):
... for f in files:
... print(os.path.join(paths,f)) #得到所有目录及子目录下的文件
...
/root/test/test.py
/root/test/day1/day1.py
/root/test/day1/base.py
/root/test/day2/day2.py
/root/test/day2/day3/a.txt>>> for paths,dirs,files in os.walk('/root/test'):
... for d in dirs:
... print(os.path.join(paths,d)) #得到所有的目录和子目录
...
/root/test/day1
/root/test/day2
/root/test/day2/day3os.walk()
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,702
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741