Reputation: 107
I'm trying the second practice project in chapter 9 of Automate the Boring Stuff with Python but even though it prints the expected strings, it does not carry out the os.unlink(filename) command, the files are not deleted, they still remain intact. Can anyone help with this? Here is the code I used:
#! python3
# deleteUnneeded.py - Searches and deletes files and folders
# to free up space on a computer
import os
# Define a function
def delUnneeded(folder):
folder = os.path.abspath(folder)
# Walk the folder tree with os.walk()
# and search for files and folders of more than 100MB using os.path.getsize()
for foldername, subfolders, filenames in os.walk(folder):
for filename in filenames:
fileSize = os.path.getsize(foldername + '\\' + filename)
if int(fileSize) < 100000000:
continue
os.unlink(filename)
print('Deleting ' + filename + '...')
delUnneeded('C:\\Users\\DELL\\Desktop\\flash')
print('Done')
Upvotes: 0
Views: 1553
Reputation: 33
This is how I solved this problem, since the question only asks to list the files larger than 100MB, we can skip the deletion part.
#! python3
# delete_files.py - Don't get misled by the program name, hah. This program
# lists the files in a folder tree larger than 100MB and prints them to the screen.
import os
folder = os.path.abspath(input('Please enter folder to search for files larger than 100MB:\n'))
if os.path.isdir(folder) is True:
i = 0 # Placeholder for number of files found larger than 100MB
for foldername, subfolders, filenames in os.walk(folder):
for file in filenames:
abs_path = os.path.abspath(foldername)
full_path = os.path.join(foldername, file)
file_size = os.path.getsize(full_path)
if file_size > 100000000:
i += 1
print(full_path)
print(f'{i} files found larger than 100MB')
else:
print('Folder does not exist.')
Upvotes: 0
Reputation: 60143
This code is the problem:
if int(fileSize) < 100000000:
continue
os.unlink(filename)
Right before you call os.unlink
, you have a continue
statement, which jumps to the next iteration of the loop.
I think you meant to have os.unlink
outside that conditional. Just unindent it:
if int(fileSize) < 100000000:
# skip small files
continue
# Otherwise, delete the file
os.unlink(filename)
UPDATE
As pointed out in the comments above, you also need to construct a full path:
os.unlink(os.path.join(foldername, filename))
UPDATE 2
Rather than if ...: continue
, you can reverse the logic of the conditional to simplify the code. Here's my cleaned up version of your code:
import os
def del_unneeded(folder):
# Walk the folder tree with os.walk() and search for files of more than
# 100MB using os.path.getsize()
for dirpath, dirnames, filenames in os.walk(folder):
for filename in filenames:
full_path = os.path.join(dirpath, filename)
if os.path.getsize(full_path) > 100000000:
print('Deleting {}...'.format(full_path))
os.unlink(full_path)
del_unneeded('C:\\Users\\DELL\\Desktop\\flash')
print("Done")
Other minor changes:
int(...)
since os.path.getsize
already returns an int
.os.walk
.os.walk
documentation and Python coding style (snake_case
rather than camelCase
).str.format
rather than string concatenation.Upvotes: 2