Python 连接路径和文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40596748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:42:59  来源:igfitidea点击:

Concatenate path and filename

python

提问by nina_berlini

I have to build the full path together in python. I tried this:

我必须在python中一起构建完整路径。我试过这个:

filename= "myfile.odt"

subprocess.call(['C:\Program Files (x86)\LibreOffice 5\program\soffice.exe',
                    '--headless',
                    '--convert-to',
                    'pdf', '--outdir',
                    r'C:\Users\A\Desktop\Repo\',
                    r'C:\Users\A\Desktop\Repo\'+filename])

But I get this error

但我收到这个错误

SyntaxError: EOL while scanning string literal.

语法错误:扫描字符串文字时 EOL。

回答by zanseb

Try:

尝试:

import os
os.path.join('C:\Users\A\Desktop\Repo', filename)

The os module contains many useful methods for directory and path manipulation

os 模块包含许多有用的目录和路径操作方法

回答by zvone

Backslash character (\) has to be escaped in string literals.

反斜杠字符 ( \) 必须在字符串文字中转义。

  • This is wrong: '\'
  • This is correct: '\\'- this is a string containing one backslash
  • 这是错误的: '\'
  • 这是正确的:'\\'- 这是一个包含一个反斜杠的字符串

Therefore, this is wrong:

因此,这是错误的:

'C:\Program Files (x86)\LibreOffice 5\program\soffice.exe'

There is a trick!

有窍门!

String literals prefixed by rare meant for easier writing of regular expressions. One of their features is that backslash characters do not have to be escaped. So, this would be OK:

前缀为 的字符串文字r旨在更容易编写正则表达式。它们的功能之一是不必转义反斜杠字符。所以,这没问题:

r'C:\Program Files (x86)\LibreOffice 5\program\soffice.exe'

However, that wont work for a string ending in backslash:

但是,这不适用于以反斜杠结尾的字符串:

  • r'\'- this is a syntax error
  • r'\'- 这是一个语法错误

So, this is also wrong:

所以,这也是错误的:

r'C:\Users\A\Desktop\Repo\'

So, I would do the following:

所以,我会做以下事情:

import os
import subprocess


soffice = 'C:\Program Files (x86)\LibreOffice 5\program\soffice.exe'
outdir = 'C:\Users\A\Desktop\Repo\'
full_path = os.path.join(outdir, filename)

subprocess.call([soffice,
                 '--headless',
                 '--convert-to', 'pdf',
                 '--outdir', outdir,
                 full_path])

回答by Aaron

To build on what zanseb said, use the os.path.join, but also \ is an escape character, so your string literal can't end with a \ as it would escape the ending quote.

要建立在 zanseb 所说的基础上,请使用 os.path.join,但 \ 也是转义字符,因此您的字符串文字不能以 \ 结尾,因为它会转义结束引号。

import os
os.path.join(r'C:\Users\A\Desktop\Repo', filename)

回答by Simon Callan

The problem you have is that your raw string is ending with a single backslash. For reason I don't understand, this is not allowed. You can either double up the slash at the end:

您遇到的问题是您的原始字符串以单个反斜杠结尾。由于我不明白的原因,这是不允许的。您可以在末尾将斜杠加倍:

r'C:\Users\A\Desktop\Repo\'+filename

or use os.path.join(), which is the preferred method:

或使用os.path.join(),这是首选方法:

os.path.join(r'C:\Users\A\Desktop\Repo', filename)

回答by Armin

To anyone else stumbling across this question, you can use \to concatenate a Pathobject and str.

对于遇到此问题的任何其他人,您可以使用\来连接Path对象和str.

Use path.Pathfor paths compatible with bothUnix and Windows (you can use it the same way as I've used pathlib.PureWindowsPath).

使用path.Path与路径兼容两种Unix和Windows(你可以用同样的方式,因为我已经使用pathlib.PureWindowsPath)。

The only reason I'm using pathlib.PureWindowsPathis that the question asked specifically about Windows paths.

我使用的唯一原因pathlib.PureWindowsPath是这个问题专门询问了 Windows 路径。

For example:

例如:

import pathlib
# PureWindowsPath enforces Windows path style
# for paths that work on both Unix and Windows use path.Path
base_dir = pathlib.PureWindowsPath(r'C:\Program Files (x86)\LibreOffice 5\program')
# elegant path concatenation
myfile = base / "myfile.odt"

print(myfile)
>>> C:\Program Files (x86)\LibreOffice 5\program\myfile.odt