I need to make a tar from files in the directory Here is my code
import tarfile
import os
path = '/home/atom/Desktop/test_arch/sample.tar'
source = '/home/atom/Desktop/test_arch'
files = os.listdir(source)
files.sort()
tar = tarfile.open(path, "w")
for file in files:
print(file)
file_path = source + '/' + file
tar.add(file_path)
tar.close()
Everything works fine. The archive is created. But instead of a list of files in it. I got several subsolders:
/home/atom/Desktop/test_arch
And only in the last subfolder are my files
If I try:
tar.add(file)
It gives an Error:
FileNotFoundError: [Errno 2] No such file or directory: '1.jpg'
You should change
Current Working Directoryto work directly in directorysourceand then you can use
filenamewithoutsource/and archive will keep it withoutsource/.