在Python中构建完整路径文件名 [英] Build the full path filename in Python

查看:166
本文介绍了在Python中构建完整路径文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文件路径名传递给模块.如何从目录名称,基本文件名和文件格式字符串构建文件路径?

I need to pass a file path name to a module. How do I build the file path from a directory name, base filename, and a file format string?

呼叫时目录可能存在或不存在.

The directory may or may not exist at the time of call.

例如:

dir_name='/home/me/dev/my_reports'
base_filename='daily_report'
format = 'pdf'

我需要创建一个字符串'/home/me/dev/my_reports/daily_report.pdf'

I need to create a string '/home/me/dev/my_reports/daily_report.pdf'

手动拼接片段似乎不是一个好方法.我试过os.path.join:

Concatenating the pieces manually doesn't seem to be a good way. I tried os.path.join:

join(dir_name,base_filename,format)

但它给出了

/home/me/dev/my_reports/daily_report/pdf

推荐答案

这很好:

os.path.join(dir_name, base_filename + "." + filename_suffix)

请记住,os.path.join()存在仅是因为不同的操作系统使用不同的路径分隔符.它可以消除这种差异,因此跨平台代码不必因每种操作系统的特殊情况而混乱.对于文件名扩展名",不需要这样做. (请参见脚注),因为在每个OS上它们始终以点字符连接到名称的其余部分.

Keep in mind that os.path.join() exists only because different operating systems use different path separator characters. It smooths over that difference so cross-platform code doesn't have to be cluttered with special cases for each OS. There is no need to do this for file name "extensions" (see footnote) because they are always connected to the rest of the name with a dot character, on every OS.

如果仍然使用函数会使您感觉更好(并且您喜欢使代码不必要地复杂化),则可以执行以下操作:

If using a function anyway makes you feel better (and you like needlessly complicating your code), you can do this:

os.path.join(dir_name, '.'.join((base_filename, filename_suffix)))

如果您希望保持代码整洁,只需在后缀中添加点即可:

If you prefer to keep your code clean, simply include the dot in the suffix:

suffix = '.pdf'
os.path.join(dir_name, base_filename + suffix)

该方法也恰好与 pathlib 中的后缀约定兼容,在问了这个问题后在python 3.4中引入了它.不需要向后兼容性的新代码可以做到这一点:

That approach also happens to be compatible with the suffix conventions in pathlib, which was introduced in python 3.4 after this question was asked. New code that doesn't require backward compatibility can do this:

suffix = '.pdf'
pathlib.PurePath(dir_name, base_filename + suffix)

如果您仅处理本地操作系统的路径,则可能更喜欢使用较短的Path而不是PurePath.

You might prefer the shorter Path instead of PurePath if you're only handling paths for the local OS.

警告:请勿为此使用pathlib的with_suffix().如果该方法包含点,它将破坏base_filename.

Warning: Do not use pathlib's with_suffix() for this purpose. That method will corrupt base_filename if it ever contains a dot.

脚注:在Micorsoft操作系统之外,没有文件名扩展名"之类的东西.它在Windows上的存在来自MS-DOS和FAT,它们是从CP/M借来的,它已经死了几十年.我们中许多人习惯看到的点加三字母只是每个其他现代OS上文件名的一部分,在这里它没有内置的含义.

Footnote: Outside of Micorsoft operating systems, there is no such thing as a file name "extension". Its presence on Windows comes from MS-DOS and FAT, which borrowed it from CP/M, which has been dead for decades. That dot-plus-three-letters that many of us are accustomed to seeing is just part of the file name on every other modern OS, where it has no built-in meaning.

这篇关于在Python中构建完整路径文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆