:art: Change to use walk

huangqimin001 1 miesiąc temu
rodzic
commit
6a7865b4bd
1 zmienionych plików z 23 dodań i 24 usunięć
  1. 23 24
      commands/management/commands/qiniuuncompressed.py

+ 23 - 24
commands/management/commands/qiniuuncompressed.py

@@ -10,29 +10,28 @@ from utils.qiniucdn import upload_file_path
10 10
 class Command(CompatibilityBaseCommand):
11 11
     def handle(self, *args, **options):
12 12
 
13
-        dirs = ['media/user/member_activity/7YmTP98A9qBdtTg5VdxTv5/contribution/edbFSK5', 'media/qn/test']
13
+        dirs = ['media/user/member_activity', 'media/qn']
14 14
 
15 15
         for dir in dirs:
16
-            try:
17
-                files = os.listdir(dir)
18
-            except Exception as e:
19
-                print(e)
20
-                continue
21
-
22
-            for file in files:
23
-                print(file)
24
-                ext = file.split('.')[-1]
25
-                if not ext:
26
-                    continue
27
-                if ext.lower() not in ['jpg', 'jpeg', 'png', 'heic']:
28
-                    continue
29
-                try:
30
-                    file_path = '{}/{}'.format(dir, file)
31
-                    if os.path.exists(file_path):
32
-                        qiniu_key = 'uncompressed/{}'.format(file_path)
33
-                        # print(file_path)
34
-                        # print(qiniu_key)
35
-                        upload_file_path(file_path, key=qiniu_key, bucket='tamron', compress=False)
36
-                        os.remove(file_path)
37
-                except Exception:
38
-                    pass
16
+            for root, dirs, files in os.walk(dir):
17
+                print('')
18
+                print('Root: {}'.format(root))
19
+                print('Dirs: {}'.format(dirs))
20
+                print('Files: {}'.format(files))
21
+                for file in files:
22
+                    print('  >> File: {}'.format(file))
23
+                    ext = file.split('.')[-1]
24
+                    if not ext:
25
+                        continue
26
+                    if ext.lower() not in ['jpg', 'jpeg', 'png', 'heic']:
27
+                        continue
28
+                    try:
29
+                        file_path = '{}/{}'.format(root, file)
30
+                        if os.path.exists(file_path):
31
+                            qiniu_key = 'uncompressed/{}'.format(file_path)
32
+                            print('    >> File Path: {}'.format(file_path))
33
+                            print('    >> Qiniu Path: {}'.format(qiniu_key))
34
+                            # upload_file_path(file_path, key=qiniu_key, bucket='tamron', compress=False)
35
+                            # os.remove(file_path)
36
+                    except Exception:
37
+                        pass