Python Forum
Creating multiple folders and other questions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating multiple folders and other questions
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sub-folders in folders from text line jenost 1 1,532 Mar-31-2020, 07:16 AM
Last Post: ndc85430
  Creating multiple lists from file without long for loop Curtnos 2 4,388 Jan-28-2018, 09:11 AM
Last Post: Gribouillis
  Netmiko - add multiple files, from different folders to mail. tomikovaknin 5 5,300 Nov-26-2017, 12:55 PM
Last Post: heiner55
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 34,332 Nov-25-2017, 06:21 AM
Last Post: heiner55
  Creating folders dynamically with year/month/date sritsv 0 6,311 Oct-16-2017, 03:44 PM
Last Post: sritsv
  Creating multiple text fies from new url retrieve results wmc326 1 3,102 Jul-13-2017, 10:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020