简体   繁体   English

尝试读取tarball时具有绝对路径的FileNotFoundError

[英]FileNotFoundError with absolute path when trying to read tarball

I am trying to read from a tar-file but despite specifying the absolute path i get a FileNotFoundError . 我正在尝试从tar文件中读取,但是尽管指定了绝对路径,但仍得到FileNotFoundError

This is the relevant portion of code: 这是代码的相关部分:

1 from pathlib import Path
2
3 testPath = Path("G:/test.tar")
4 tar = tarfile.open(testPath, "r")
5 ...

and the file definitely exists. 并且该文件肯定存在。 在此处输入图片说明

But what I get is (originating from line 4): 但是我得到的是(从第4行开始):

FileNotFoundError: [Errno 2] No such file or directory: 'G:\\test.tar'

(I am using PyCharm btw.) What am I missing? (我正在使用PyCharm btw。)我缺少什么? I will gladly provide additional information if needed. 如果需要,我将很乐意提供其他信息。

Check to make sure your script/file is located in the correct directory 检查以确保您的脚本/文件位于正确的目录中

from pathlib import Path
import tarfile

testPath = Path("Songs.txt.tar")
tar = tarfile.open(testPath, "r")
print(tar) # Returns <tarfile.TarFile object at 0x100d44f98>

print(tarfile.is_tarfile("Songs.txt.tar")) # Returns True if its tar file

Since in the line number 3 you are generating file path using the following line: 由于在第3行中,您正在使用以下行生成文件路径:

testPath = Path("G:/test.tar")

testPath variable is of type pathlib.WindowsPath. testPath变量的类型为pathlib.WindowsPath。 while in the next tarfile.open requires filepath in string format. 而在下一个tarfile.open中,需要字符串格式的文件路径。

Please try following: 请尝试以下操作:

testPath = Path("G:/test.tar")
tar = tarfile.open(str(testPath), "r")

or: 要么:

testPath = str(Path("G:/test.tar"))
tar = tarfile.open(testPath, "r")

Solution: 解:

after a recent pc-reset I forgot to change explorer-view to "always show file-type-extension" again which led to me not recognizing that it should have been 在最近一次PC重置后,我忘记了再次将资源管理器视图更改为“始终显示文件类型扩展名”,这导致我不知道它应该是

test.tar.gz

as there were only other folders in this directory apart from the file in question. 因为此目录中除了该文件外仅存在其他文件夹。 So adjusting my testPath solved that. 因此,调整我的testPath解决了这一问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用绝对路径FileNotFoundError - Using absolute path, FileNotFoundError python FileNotFoundError:[Errno 2]具有绝对路径和文件 - python FileNotFoundError: [Errno 2] with absolute path and file exists FileNotFoundError:[错误2]没有这样的文件或具有绝对文件路径的目录 - FileNotFoundError: [Errno 2] No such file or directory with ABSOLUTE file path 当试图读取数据块中的文件时,我得到 IllegalArgumentException: Path must be absolute - when trying to read a file in databricks i get IllegalArgumentException: Path must be absolute Databricks 上的 PySpark 在绝对 URI 中获取相对路径:尝试使用 DateStamps 读取 Json 文件时 - PySpark on Databricks getting Relative path in absolute URI: when trying to read in Json Files with DateStamps 指定正确路径时出现FileNotFoundError - FileNotFoundError when specifying correct path 尝试安装PyICU时出现FileNotFoundError - FileNotFoundError when trying to install PyICU Elastic Beanstalk无法识别文件的绝对路径,返回FileNotFoundError - Elastic Beanstalk won't recognize absolute path to file, returns FileNotFoundError FileNotFoundError尝试在Pycharm中使用Jupyter读取数据文件 - FileNotFoundError trying to read a datafile with Jupyter in Pycharm 路径正确时 django 管道 FileNotFoundError - django pipeline FileNotFoundError when the path is right
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM