I'm writing a Python script with the goal of compressing all files in a directory and sub-directories and having them placed in those directories. However, I'm running into a problem. Currently, the program will compress files in sub-directories, but it will do it multiple times. In fact, the factor is equivalent to the number of sub-directories that exist.
For example, say I have a directory as follows:
dir
-folder1
--file1
-folder2
--file2
file3
It would compress file1 and file2 twice. My script is as follows:
import subprocess, os
path = 'directory'
for dirpath, diames, files in os.walk(path):
for filename in files:
p = subprocess.Popen(['C:\Program Files (x86)\7-Zip\7z.exe', 'a', '-mx1', filename+'.7zip', path+filename, '-r'], cwd = dirpath)
p.wait()
Any ideas why this happening? I'm thinking it may be due to the two for-loops, but I'm not sure what it could be.
Recent Questions...
ما را در سایت Recent Questions دنبال میکنید
برچسب:
نویسنده: استخدام کار
بازدید: 262
تاريخ: پنجشنبه
20 خرداد
1395 ساعت: 2:34