扫码关注官方订阅号
84669人学习
65727人学习
82984人学习
467778人学习
498837人学习
471966人学习
256484人学习
152542人学习
224170人学习
139536人学习
81804人学习
85022人学习
11944人学习
20001人学习
60816人学习
5487人学习
15007人学习
2150人学习
6980人学习
194925人学习
359900人学习
1142人学习
19058人学习
3206人学习
180550人学习
48569人学习
17603人学习
40936人学习
1049人学习
750人学习
32909人学习
使用Python怎么查看文件名和文件路径?
使用Python怎么查看文件名和文件路径?-PHP中文网问答-使用Python怎么查看文件名和文件路径?-PHP中文网问答
围观一下哦,学习一下。
>>> import os >>> url = 'http://images.cnitblog.com/i/311516/201403/020013141657112.png' >>> filename = os.path.basename(url) >>> filepath = os.path.dirname(url) >>> filename '020013141657112.png' >>> filepath 'http://images.cnitblog.com/i/311516/201403' >>>
import os print(os.path.realpath(__file__)) # 当前文件的路径 print(os.path.dirname(os.path.realpath(__file__))) # 从当前文件路径中获取目录 print(os.path.basename(os.path.realpath(__file__))) # 从当前文件路径中获取文件名 print(os.listdir(dirname)) # 只显示该目录下的文件名和目录名,不包含子目录中的文件,默认为当前文件所在目录
import os # os.walk()遍历文件夹下的所有文件 # os.walk()获得三组数据(rootdir, dirname,filnames) def file_path(file_dir): for root, dirs, files in os.walk(file_dir): print(root, end=' ') # 当前目录路径 print(dirs, end=' ') # 当前路径下的所有子目录 print(files) # 当前目录下的所有非目录子文件
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
精品班
技术支持
技术咨询
学习群
会员优惠
返回顶部
使用Python怎么查看文件名和文件路径?-PHP中文网问答-使用Python怎么查看文件名和文件路径?-PHP中文网问答
围观一下哦,学习一下。