当文件夹名称中有空格时使用`os.path.exists`?

purval_patel

你好,我正在使用以下代码,但它不起作用。这是我的代码的一部分。userisfolder2变量的随机来自我的应用程序。

但这段代码只进入 else 条件。这个目录是存在的。

useris = "user"
folder2 = "/'Personal Files'/Docs"
if os.path.exists("/usr/bin/.%s/c_drive%s/test.exe"%(useris,folder2)):
    print "folder exists"
else:
    print "folder not exists"

提出解决方案。

cs95

去掉 周围的单引号Personal Files引号只供shell 解释和理解,但os.path没有它们也能理解和解析空格。

所以,如果我的桌面上有一个名为“深渊制造”的文件夹,我会这样做:

In [858]: os.path.exists('/Users/coldspeed/Desktop/Made in Abyss')
Out[858]: True

所以,在你的情况下,这应该有效:

useris = "user"
folder2 = "/Personal Files/Docs"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

python os.path.exists 返回 false

os.path.exists()始终返回false

使用os.path.exists和assert来检查除已定义列表之外是否还存在其他文件夹

os.path.exists()将创建该文件

os.path AttributeError:'str'对象没有属性'exists'

尝试通过变量使用 os.path.exists 但出现错误

名称中有空格的文件夹无法识别

Files.exists(path)和path.toFile()。exists()对同一文件给出不同的结果

用户名有空格时使用os.path.expanduser

os.path.exists与os.path.isdir之间的优缺点

os.path.exists返回错误的w /包含空格的转义路径

Windows 10上的Python:os.rename()找不到os.path.exists()找到的文件吗?

Shell脚本:如果path或filename中有空格,则ffmpeg无法读取参数或文件名

python 函数 os.path.exists() 当我给一个 bool 值时返回 True

python os.path.exists()对于存在的nfs挂载目录文件失败

os.path.exists对Windows上名为“ CON.csv”的文件提供误报

删除 50 MB 以下的小文件夹,名称中有空格

尝试进行符号链接,但文件夹名称中有空格

当路径中的文件夹中有空格时,无法直接从文件夹中打开PowerShell文件

错误出现在这一行 os.path.exists()

os.path.exists() 在 Windows 中引发错误,而不是在 Pycharm 或 Ubuntu 中

即使path.exists()返回true,Subprocess.run()也找不到文件

如何修补`pathlib.Path.exists()`方法?

%~dp0 在名称中有空格的文件夹下使用时不起作用

python3使用os.path.exists检查并移动,并使用通配符或与数字匹配的shutil.move

从文本文件获取“字符串(路径)”时,File.Exists(Path)始终返回false

如何从Path中提取文件夹名称

os.path.isdir()向现有文件夹返回false

OS X查找和RM通过名称中带有空格的文件夹递归通过吗?