os.path.dirname(path)
去掉文件名,返回目录
os.path.dirname(_file_):
- 当"print os.path.dirname(_file_)"所在脚本是以完整路径被运行的,将输出该脚本所在的完整路径
- 当"print os.path.dirname(_file_)"所在脚本是以相对路径被运行的, 将输出空目录
示例:
import os
def read_file(file_name,save_name):
#read info from file_name
with open(os.path.join(
os.path.dirname('__file__'),
file_name)) as file:
file_info = file.read()
#save as save_name
with open(save_name,'w') as file:
save_name=file.write(file_info)
read_file('test of pygal.svg','copy_test of pygal.svg')
博客介绍了Python中os.path.dirname(path)函数的作用,即去掉文件名返回目录。还说明了os.path.dirname(__file__)在脚本以完整路径和相对路径运行时的不同输出情况,并给出了获取当前文件路径的示例。
2080

被折叠的 条评论
为什么被折叠?



