- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 OS 模块打开文件进行读取,但出现 FileNotFoundError。
我正在努力
当我尝试打开时,出现以下错误:
File "parse_mda_SIC.py", line 16, in <module>
f = open(file, 'r')
FileNotFoundError: [Errno 2] No such file or directory:
'mda_3357_2017-03-08_1000230_000143774917004005__3357.txt'
我怀疑问题要么与"file"变量有关,要么是它在一个目录下,但我很困惑为什么当我使用操作系统来处理该较低目录时会发生这种情况。
我有以下代码:
working_dir = "data/"
for file in os.listdir(working_dir):
if (file.find("mda") != -1):
SIC = re.findall("__(\d+)", file)
f = open(file, 'r')
我希望能够毫无问题地打开文件,然后根据数据创建我的列表。感谢您的帮助。
最佳答案
这应该适合您。您需要附加该目录,因为它仅将其视为代码顶部的文件名,并且只会在您的代码所在的目录中查找该文件名。
for file in os.listdir(working_dir):
if (file.find("mda") != -1):
SIC = re.findall("__(\d+)", file)
f = open(os.path.join(working_dir, file), 'r')
另外,使用 with
的上下文管理器打开文件也是一个好习惯,因为它会在不再需要文件时处理关闭文件:
for file in os.listdir(working_dir):
if (file.find("mda") != -1):
SIC = re.findall("__(\d+)", file)
with open(os.path.join(working_dir, file), 'r') as f:
# do stuff with f here
关于python - 尝试打开文件以在 Python 3 中读取时出现 FileNotFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55895254/
from os import path from pydub import AudioSegment input_file = "audio.mp3" output_file = "result.wa
尝试运行 mask_rcnn 的示例程序颜色飞溅并收到此错误: 我正在环境中的 anaconda 上运行 mrcnn 模型masknet,并安装了以下软件包。我已按照 https://github
我正在尝试使用 PySerial 来接受来自 RFID 阅读器的输入。根据 here: 的答案,我尝试使用 WinObj 并发现一些奇怪的东西:COM3 文件夹中没有 GLOBAL??? 端口指向“更
我有一个奇怪的问题。我既不能重命名特定文件,也不能删除它们。我收到 FileNotFoundError。 以前也有人问过类似的问题。此问题的解决方案是使用完整路径,而不仅仅是文件名。 我的脚本在仅使用
我有一个文本文件“Flickr_8k.testImages.txt”,其中包含由换行符分隔的 1000 个文件的文件名。这些文件位于目录“Flickr8k_Dataset”内,其中包含 8000 多个
我正在尝试制作一个简单的程序,将具有用户指定扩展名的所有文件复制到另一个文件夹中。代码如下,错误如下: #! python3 # Copies all files from folder with u
我的 Python 应用程序包含一个名为 Tests 的子文件夹,我用它来运行单元测试。我的所有文件都位于父文件夹中,我将其称为 App。例如,Tests 文件夹包含一个 test.py 文件。 Ap
我有一个函数,它获取包含时间的源文件(csv 文件),读取它,然后按顺序对行进行排序并将它们写入目标文件中。但是,如果源 csv 文件不存在,我需要引发 FileNotFoundError。我之前曾引
我收到 FileNotFoundError当用户尝试注册我的网站并上传照片时出现错误消息。错误消息如下: [Errno 2] No such file or directory: '/tmp/.upl
我正在尝试关注 this blog关于如何从 Python 执行 R 脚本。我使用 Rscript 从命令行运行 R 脚本。 这是我的 Python 代码: import subprocess imp
使用 Windows 7、Python 3.5.1: import subprocess subprocess.check_output(['echo', 'hello']) 引发错误: Traceb
这个问题在这里已经有了答案: open() gives FileNotFoundError/IOError: Errno 2 No such file or directory (8 个答案) 关闭
我使用的第三方库很好,但不会按照我想要的方式处理不存在的文件。当给它一个不存在的文件时,而不是提高旧的 FileNotFoundError: [Errno 2] No such file or dir
我已经使用 matplotlib 制作了一个图形,现在我正在尝试使用 matplotlib.pyplot.savefig 将其保存到文件中 import matplotlib.pyplot as pl
您好,我正在关注 pygame 游戏代码,但出现以下错误并发布了一个问题。当我运行它时,屏幕弹出然后立即消失。背景颜色也没有保存就执行了。 (Mac 操作系统) import pygame pygam
刚刚回到Python,我正在尝试构建一个脚本来匹配文件名,重命名,压缩它们,然后最终构建一个控制文件(我还没有写过)。它适用于放置在目录中的文件,但最后我收到错误: FileNotFoundError
我在使用 Python 的 Mac OS X 中遇到了相当奇怪的竞争条件(我只测试过 Python 3.3)。我正在创建几个临时目录,向其中写入内容,然后清除它们。类似于 while running:
为什么下面的交互失败? (Python 3.6.1) >>> with open('an_image.png', 'rb') as f, open('~/Desktop/an_image.png',
img = printscreen_pil img = img.filter(ImageFilter.MedianFilter()) enhancer = ImageEnhance.Contrast(
使用 unittest 模块,如何测试打开文件时引发的 FileNotFoundError,如下所示: func(filename): try: file = open(fil
我是一名优秀的程序员,十分优秀!