how to delete files from command line 1

how to delete files from command line

rm filename

Here is what the above code is Doing:
1. We’re importing the os module, which contains functions for interacting with the operating system.
2. We’re getting the current working directory using os.getcwd() and storing it in a variable called cwd.
3. We’re using os.listdir() to get a list of everything in the current working directory and storing it in a variable called files.
4. We’re looping over each file in files.
5. We’re checking whether the file ends with .txt using the string method endswith().
6. If the file ends with .txt, we’re printing the filename.
7. If the file doesn’t end with .txt, we’re using os.unlink() to delete it.

Similar Posts