[英]FileNotFoundError with absolute path when trying to read tarball
我正在嘗試從tar文件中讀取,但是盡管指定了絕對路徑,但仍得到FileNotFoundError 。
這是代碼的相關部分:
1 from pathlib import Path
2
3 testPath = Path("G:/test.tar")
4 tar = tarfile.open(testPath, "r")
5 ...
但是我得到的是(從第4行開始):
FileNotFoundError: [Errno 2] No such file or directory: 'G:\\test.tar'
(我正在使用PyCharm btw。)我缺少什么? 如果需要,我將很樂意提供其他信息。
檢查以確保您的腳本/文件位於正確的目錄中
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
由於在第3行中,您正在使用以下行生成文件路徑:
testPath = Path("G:/test.tar")
testPath變量的類型為pathlib.WindowsPath。 而在下一個tarfile.open中,需要字符串格式的文件路徑。
請嘗試以下操作:
testPath = Path("G:/test.tar")
tar = tarfile.open(str(testPath), "r")
要么:
testPath = str(Path("G:/test.tar"))
tar = tarfile.open(testPath, "r")
解:
在最近一次PC重置后,我忘記了再次將資源管理器視圖更改為“始終顯示文件類型擴展名”,這導致我不知道它應該是
test.tar.gz
因為此目錄中除了該文件外僅存在其他文件夾。 因此,調整我的testPath解決了這一問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.