|
19 | 19 |
|
20 | 20 |
|
21 | 21 | # Exercise 2 |
22 | | -# Display the full paths of any PNG files in the "images" folder |
| 22 | +# Display the full paths of any `*.png` files in the "images" folder |
23 | 23 | file_matches = os.path.join(path, "*.png") |
24 | 24 | print('All PNG files in "images" folder:') |
25 | 25 | for file_name in glob.glob(file_matches): |
26 | 26 | print(file_name) |
27 | 27 |
|
28 | 28 |
|
29 | 29 | # Exercise 3 |
30 | | -# Change all PNGs to JPGs in the "images" folder and its subfolders |
31 | | -# Could use indexing to get the file extension, but try using |
32 | | -# os.path.splitext() |
| 30 | +# Rename all `*.png` files in the "images" folder and its subfolders |
| 31 | +# to `*_backup.png` |
33 | 32 | for current_folder, subfolders, file_names in os.walk(path): |
34 | 33 | for file_name in file_names: |
35 | 34 | file_path = os.path.join(current_folder, file_name) |
36 | | - file_tuple = os.path.splitext( |
37 | | - file_path |
38 | | - ) # split into (path, extension) |
39 | | - if file_tuple[1].lower() == ".png": # check if extension is PNG |
40 | | - pass # os.rename(file_path, file_tuple[0] + ".jpg") |
| 35 | + if file_path.lower().endswith(".gif"): |
| 36 | + new_path = file_path[-4] + "_backup.gif" |
| 37 | + os.rename(file_path, new_path) |
41 | 38 |
|
42 | 39 |
|
43 | 40 | # Exercsie 4 |
44 | 41 | # Check that the two files have been converted to JPGs successfully |
45 | | -print(os.path.exists(os.path.join(path, "png file - not a gif.jpg"))) |
| 42 | +print(os.path.exists(os.path.join(path, "png file - not a gif_backup.gif"))) |
46 | 43 | print( |
47 | | - os.path.exists(os.path.join(path, "additional files/one last image.jpg")) |
| 44 | + os.path.exists( |
| 45 | + os.path.join(path, "additional files/one last image_backup.gif") |
| 46 | + ) |
48 | 47 | ) |
49 | 48 |
|
50 | 49 |
|
|
0 commit comments