Python Scripts: Difference between revisions

Line 848:
</syntaxhighlight>
 
 
== File Backups Section ==
<syntaxhighlight lang="python">
#!/usr/bin/env python3
# Code for Archiving existing files & save list of files
 
# Save File in Dest address
 
import os
import tarfile
import sys
 
directory = sys.argv[1]
 
wr = open("file_list.txt", "w")
wr.write("List of files:\n")
wr.write("\n")
wr.close()
 
for root, dirs, files in os.walk(directory):
for name in files:
file = root+"/"+name
wr = open("file_list.txt", "a")
wr.write(name)
wr.write("\n")
wr.close()
 
print("Done saving list of files")
 
 
for root, dirs, files in os.walk(directory):
tardir = root + '.backup.tar.gz'
tar = tarfile.open(tardir, "w:gz")
for name in files:
file = root+"/"+name
tar.add(file)
tar.close()
 
print("Done Backing up files")
</syntaxhighlight>
 
<br />