[英]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.