- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
结合我的last question ,我将在某种列表中打印文件名及其旁边的大小。基本上,我正在从一个文件(由用户添加)中读取文件名,获取文件名并将其放入工作目录的路径中以一一打印其大小,但是我遇到了以下问题 block :
print("\n--- Stats ---\n")
with open('userdata/addedfiles', 'r') as read_files:
file_lines = read_files.readlines()
# get path for each file and find in trackedfiles
# use path to get size
print(len(file_lines), "files\n")
for file_name in file_lines:
# the actual files should be in the same working directory
cwd = os.getcwd()
fpath = os.path.join(cwd, file_name)
fsize = os.path.getsize(fpath)
print(file_name.strip(), "-- size:", fsize)
返回此错误:
tolbiac wpm-public → ./main.py --filestatus
--- Stats ---
1 files
Traceback (most recent call last):
File "./main.py", line 332, in <module>
main()
File "./main.py", line 323, in main
parseargs()
File "./main.py", line 317, in parseargs
tracking()
File "./main.py", line 204, in tracking
fsize = os.path.getsize(fpath)
File "/usr/lib/python3.4/genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [Errno 2] No such file or directory: '/home/tolbiac/code/wpm-public/file.txt\n'
tolbiac wpm-public →
所以看起来好像有东西在 file_name 的末尾添加了一个\n ,我不确定这是否是 getsize 模块中使用的东西,我用 os.stat 尝试了这个,但它做了同样的事情。
有什么建议吗?谢谢。
最佳答案
当您读取文件时,您需要了解数据是如何分隔的。在这种情况下,读入文件每行都有一个由 \n
运算符分隔的文件名。使用前需要将其剥离。
for file_name in file_lines:
file_name = file_name.strip()
# rest of for loop
关于python - 将 os.path.join 与 os.path.getsize 一起使用,返回 FileNotFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30085360/
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
我是一名优秀的程序员,十分优秀!