0

Hey there I tried making a loop for creating folders and files. According to me the program should work like this :

  1. create 1st folder and 1 file inside it.
  2. create 2nd folder and 1 file inside it

and it goes on..... What my program does is :

  1. create 1 file
  2. create folders

my program

import os,sys

di=("ab")
a=0
i=0
fi=open("az.txt","w")

def file():
    for i in range(0,10):
        fi.write(str(i))

def mk():
    for a in range(0,10):
        os.mkdir(di+str(a))
        file()

mk()

please help

4
  • Simple debugging: put print statements inside the various loops (make their strings unique) and follow the flow of the program. Commented May 15, 2014 at 12:49
  • NB: file is a built-in Python function. You're overriding it, so you'll be using your function, but better rename it, to something like create_files. Commented May 15, 2014 at 12:50
  • @Torxed the content of az.txt is the valuse of i Commented May 15, 2014 at 12:52
  • @Evert thanks I did change the function but it had no effect the file is still being created outside the function Commented May 15, 2014 at 13:00

2 Answers 2

1

This script will create a folders with the name "a", "b", "c" and put files 1.txt 2.txt .. 5.txt in each folder.

Make a changes as you need as try it.

import os
for i in "abc":
    os.system ("mkdir "+i)
    for j in range (5):
            os.system ("touch "+str(i)+"/"+str(j)+".txt")
Sign up to request clarification or add additional context in comments.

3 Comments

thanks it worked :) but can you please explain me how it works
which statement you want to know? os.system - is running bash command in terminal. When you comabine "mkdir "+i - actualy you run command "mkdir a" after that you build and run bash command to create a file "touch a/1.txt" Basic bash commands
if you need to put some content in some file - just put in right position the command : os.system("echo blabla > somefile.txt") OR if the content is known and the name of the file is known : os.system("echo "+content+" > "+filename).Please make sure you have spaces on right place while building the command
1

Hey there I tried making a loop for creating folders and files. According to me the program should work like this : 1) create 1st folder and 1 file inside it. 2) create 2nd folder and 1 file inside it and it goes on..... What my program does is : 1) create 1 file 2) create folders

Indeed, it does the latter and not the former. What your program does is create directories, and write:

0123456789

ten times inside az.txt. Your error is that you're opening a file outside of any loop, and then you write to it within a loop.

I guess, that's what you want:

import os,sys

di="ab"

def mk_file(di):
    for i in range(0,10):
        with open("{}/az_{}.txt".format(di,i), "w") as fi:
            fi.write(str(i))

def mk_dir():
    for a in range(0,10):
        dname = "{}_{}".format(di, str(a))
        os.mkdir(dname)
        mk_file(dname)

if __name__ == "__main__":
    mk_dir()

3 Comments

thanks for the answer but your program does the same thing it creates the files outside the folder I wanted the files to be created inside the folder
sorry, forgot a detail: it's to open the file within the folder :-) Corrected in last edit.
yup, I was'nt giving the name used to create the file in mk_file. Sorry again. This time I tested the code and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.