The code looks for the specified .tgz file, if it's not present, it creates it. It looks for files ending in .txt in the cwd and adds them to a list. It decompresses the gzip, checks for the .txt files in the tar, adds the missing files to the tar and then recreates the compressed gzip with the new files in it. It then deletes the temporarily created tar file.
This is a self assigned script to learn the basics of working with compressed tarballs.
Please take a look and let me know if there are problems that can be accounted for or improvements that can be made. I'm only a few weeks into python, there is still a lot that I don't know.
#!usr/bin/python3
import os, gzip
import tarfile
import sys
import shutil
import send2trash
myFL = []
notThere = []
feil = '/home/pi/python/chap9OrganizingFiles/shutildir/comp.tgz'
outfeil = '/home/pi/python/chap9OrganizingFiles/shutildir/comp.tar'
if os.path.isfile(feil):
print('File found, opening ' + os.path.relpath(feil))
else:
print('File does NOT exist, creating ' + os.path.relpath(feil))
with tarfile.open(feil, 'w:gz'): print('File Created')
with tarfile.open(feil, 'r:gz') as tar:
myDirList = os.chdir('/home/pi/python/chap9OrganizingFiles/shutildir/')
print('Current directory is: ' + os.getcwd() + '\n\n')
for dirList in os.listdir(os.getcwd()):
myFL.append(dirList)
for listItem in myFL:
if listItem in tar.getnames():
print("There: " + listItem)
else:
if listItem.endswith('.txt'):
notThere.append(listItem)
print('\n\n')
for li in notThere: print('Text file not in tarball: ' + li)
with gzip.open(feil, 'rb') as unzip, open(outfeil, 'wb') as out:# open the gzip file
out.write(unzip.read()) # read the gzip file into the output file
def addToTar(ntlist):
with tarfile.open(outfeil, 'a') as tar:
for ntLI in ntlist:
tar.add(ntLI)
print('Item added: ' + ntLI)
addToTar(notThere)
with open(outfeil, 'rb') as uf, gzip.open('/home/pi/python/chap9OrganizingFiles/shutildir/comp.tgz', 'wb') as of:
shutil.copyfileobj(uf, of)
send2trash.send2trash(outfeil)
print('\nAll done and cleaned up.')
-
\$\begingroup\$ Not making these procedures into functions makes it difficult to reuse your code. \$\endgroup\$Aaron Hall– Aaron Hall2016年01月30日 17:10:25 +00:00Commented Jan 30, 2016 at 17:10
-
2\$\begingroup\$ I have rolled back the last edit. Please see What to do when someone answers. \$\endgroup\$Peilonrayz– Peilonrayz ♦2016年01月30日 20:37:01 +00:00Commented Jan 30, 2016 at 20:37
1 Answer 1
Remove unecessary code
myDirList
is assigned but never used, delete it:
myDirList = os.chdir('/home/pi/python/chap9OrganizingFiles/shutildir/')
You do not need to build a list as:
for dirList in os.listdir(os.getcwd()):
myFL.append(dirList)
for listItem in myFL:
You can iterate over os.listdir(os.getcwd())
directly:
for listItem in os.listdir(os.getcwd()):
-
\$\begingroup\$ I'll do that, thanks for pointing that out. Appreciated. \$\endgroup\$user95110– user951102016年01月30日 19:57:05 +00:00Commented Jan 30, 2016 at 19:57
-
\$\begingroup\$ @Dimurof82 I suggest waiting some days before accepting, a better answer may come along. \$\endgroup\$Caridorc– Caridorc2016年01月31日 14:44:30 +00:00Commented Jan 31, 2016 at 14:44
-
1\$\begingroup\$ Thanks, ill see what comes along. If nothing better, its yours. \$\endgroup\$user95110– user951102016年01月31日 14:45:35 +00:00Commented Jan 31, 2016 at 14:45