I wrote a function in Python to call 7zip to compress a folder. The code works and is able to perform the task, however I do not know if this is the most optimal way. Are there any ways to improve the code, any redundant steps?
def Compress_7z(Initial_path,Container_Name,File_List, compression_level = -1,DAC = False): """Compression level = -1 standard, 0-9 compression level DAC : Delete After Compression - Remove File after being compressed """ try: chdir(Initial_path) except: return('Path does not exist') # write a listfile with open('list.txt', 'w') as f: for item in File_List: f.write("%s\n" % item) cmd = ['7z', 'a', Container_Name,'@list.txt'] if compression_level != -1 and compression_level <= 9: cmd.append('-mx{0}'.format(compression_level)) elif compression_level > 9 or compression_level < -1: return("Compression not standard: aborting compression") system = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,shell=True) while system.poll() == None: # .poll() will return a value once it's complete. time.sleep(1) if DAC: for f in File_List: remove(f) # Clean up from compression remove('list.txt') return(system.communicate())