Python Forum

Full Version: Creating multiple folders and other questions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all

As mentioned in an introduction post I am very new to Python.

My first goal is to create a program/code that will simply sorts files into respective folders.

Quick summary of what I want to achieve:

I want to create a program that moves files based on their extensions into specific folders.
A user would manually unzip 'a ton' of files into a folder.
The files have different extensions.
All of the files with the same extension need to be placed in the same folder.
Any file that is not in the list to be moved simple stays in the top folder, for example text files.

I've had a look at the os module tutorial below and was able to create the respective directories I need.

https://pythonprogramming.net/python-3-os-module/

import os
os.mkdir('Maps')
os.mkdir('Sounds')
os.mkdir('Music')
os.mkdir('Textures')
This worked just fine.
However, surely a better approach would be to create all the subfolders in one go?

I tried:

os.makedirs ('/Maps/Sounds/Music/Textures')
It didn't error but it also didn't create these folders?
Any ideas what I am doing wrong please?

Jumping the gun a little......any clues or suggestions on what to dabble with next to get files to move into folders?

Finally, once I have all the code correct, how do I package it into a tool that anyone can just run?

Thank you for any help and for your patience with a newbie Smile
The only way I can think of to make this code simpler is to use a loop:

for dir in ('Maps', 'Sounds', 'Music', 'Textures'):
    os.mkdir(dir)
os.makedirs failed because it makes directories down the file tree (as needed), not across it. So it makes a Maps directory, then a Sounds directory in the Maps directory, and so on.
(Aug-19-2018, 06:43 PM)ichabod801 Wrote: [ -> ]The only way I can think of to make this code simpler is to use a loop:

for dir in ('Maps', 'Sounds', 'Music', 'Textures'):
    os.mkdir(dir)
os.makedirs failed because it makes directories down the file tree (as needed), not across it. So it makes a Maps directory, then a Sounds directory in the Maps directory, and so on.

Hi there

Big thanks for the reply. I'm not familiar with loops but will look into it. Just tested your code and it works perfectly. I understand your explanation of makedirs and why it failed - thank you. Baby steps for me Smile