I am trying to copy all the .txt files from a compressed file (this file has unknown_name_folders): .txt files location ---> my_path/?unknown_name_folder?/file.txt I want to do ---> my_path/file.txt
I did this code, but I have this error: EOFError: Compressed file ended before the end-of-stream marker was reached. Any idea?
file=my_path+"/"+fil
if file.endswith('.tar.gz'):
tarf = tarfile.open(file, "r:gz")
for info in tarf:
if info.name.endswith('.txt'):
print(info.name)
tar = tarfile.open(file) #extracting
tar.extractall()
tar.close()
code=os.system('cp ' + my_path+'/'+info.name +' '+ file)
As Paulo mentioned, you missed off the "gz" flag the second time you opened the tar file. Also, your target for copying the file was the original tarball - I assume you wanted it in a separate directory ("targdir" here)
You only need to extract the contents once, so the code becomes:
file=my_path+"/"+fil
targdir='dest'
if file.endswith('.tar.gz'):
tarf = tarfile.open(file, "r:gz")
tarf.extractall()
for info in tarf:
if info.name.endswith('.txt'):
print(info.name)
code=os.system('cp ' + info.name +' '+ targdir)
(edit - you don't need 'mypath' in the "cp" command - you have extracted the tarball into the current directory, so "info.name! is the full path to the file)
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.