Python: os.path.isfile won#39;t recognise files beginning with a number(Python:os.path.isfile 无法识别以数字开头的文件)
问题描述
所以,我正在尝试将 os.path.isfile 或 os.path.exists 合并到我的代码中,并成功找到某些常规文件(pdf,png) 搜索以字母开头的文件名时.
So, I'm trying to incorporate os.path.isfile or os.path.exists into my code with success in finding certain regular files(pdf,png) when searching for filenames that begin with a letter.
我正在使用的文件命名标准(并且由于用户而无法更改)以数字开头,随后无法使用相同的方法找到.有没有办法让 .isfile 或 .exists 发现这些文件?
The file naming standard that I'm using (and can't change due to the user) starts with a number and subsequently can't be found using the same method. Is there a way that I can make these files discoverable by .isfile or .exists?
我要搜索的文件是 .txt 文件.
The files I'm searching for are .txt files.
os.path.isfile("D:Usersspx9gsProject WorkData21022013AA.txt")
os.path.isfile("D:Usersspx9gsProject WorkDataAA21022013.txt")
返回:
错误
是的
推荐答案
你需要使用原始字符串,或者转义你的反斜杠.在文件名中:
You need to use raw strings, or escape your backslashes. In the filename:
"D:Usersspx9gsProject WorkData21022013AA.txt"
210 将被解释为八进制转义码,因此您将无法获得正确的文件名.
the 210 will be interpreted as an octal escape code so you won't get the correct filename.
以下任何一种都可以:
r"D:Usersspx9gsProject WorkData21022013AA.txt"
"D:\Users\spx9gs\Project Work\Data\21022013AA.txt"
这篇关于Python:os.path.isfile 无法识别以数字开头的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python:os.path.isfile 无法识别以数字开头的文件
基础教程推荐
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
