简体   繁体   中英

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 .

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):

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

(I am using PyCharm btw.) What am I missing? 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:

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

testPath variable is of type pathlib.WindowsPath. while in the next tarfile.open requires filepath in string format.

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

test.tar.gz

as there were only other folders in this directory apart from the file in question. So adjusting my testPath solved that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM