HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Making a directory structure for a web project

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
directoryprojectforstructurewebmaking

Problem

I have always been into HTML and CSS so I decided to give programming a try. I installed Python because I heard it is the best first programming language. So I made this program just now and I'm super excited and happy about it. I would like to have your criticism or feedback and tell me how I can make it better or add more functionality to it.

import os

name = input("Enter the name of your project here: ");

os.makedirs(name);

os.chdir(name);

os.makedirs('css');
os.makedirs('resources');

os.chdir('resources');

os.makedirs('img');
os.makedirs('js');

os.chdir('../');

html = input("Enter name of html file: ")+".html";
open(html, 'a');

os.chdir('css');

css = input("Enter name of css file: ")+".css";
open(css, 'a');

Solution

I think it would be nice, if you could define the directory structure once and then let the program create it. Something like this:

import os

folders = ["css", "resources/img", "resources/js"]

name = input("Enter the name of your project here: ")
for folder in folders:
    os.makedirs(os.path.join(name, os.path.normpath(folder)))
os.chdir(name)
...
# file opening like Peilonrayz suggested


This uses a list and os.path.join, which allows joining filenames with the platform specific file name separator (\ for Windows, / for UNIX), os.path.normpath to convert the / in the path to \ in case you are on Windows and it also uses a for-loop, if you have not seen them, yet.

Code Snippets

import os

folders = ["css", "resources/img", "resources/js"]

name = input("Enter the name of your project here: ")
for folder in folders:
    os.makedirs(os.path.join(name, os.path.normpath(folder)))
os.chdir(name)
...
# file opening like Peilonrayz suggested

Context

StackExchange Code Review Q#160037, answer score: 8

Revisions (0)

No revisions yet.