To organize files in the current directory of a Python program, we can use the os
module in combination with various directory and file operations. Here's an example code block that shows how to organize files based on their extensions:
main.py617 chars22 lines
In this code block, we first get the absolute path of the current directory using the pathlib
module. We then iterate over all files in the directory (os.listdir()
) and ignore directories and hidden files. For each file, we get its extension using pathlib.Path().suffix
and create a new directory with that extension name if it doesn't already exist using os.mkdir()
. Finally, we move the file to the corresponding directory using os.rename()
.
This is just one example of how to organize files in Python. There are many other ways and variations depending on your specific requirements.
gistlibby LogSnag