1

i was wondering if there was a way to create 10 empty files in a a specific directory while using a loop in python. I need to create the files with the names test1.txt, test2.txt, all the way up to test10.txt. I am pretty new to python, so any help with this would be greatly appreciated. Here is what i am working with.

def createFiles()
    lab3='/home/student/Lab3'
    #Before creation of files
    dir_list = os.listdir(lab3)
    print("List of directories before creating files")
    print(dir_list)
    print()
    
    with open('test1.text', 'w'):
        pass
    #After creation of files
    dir_list=os.listdir(lab3)
    print("Directory and files after file creation: ")
    print(dir_list)    

5 Answers 5

5

Just place your call to open inside a loop:

for i in range(1, 11):
    with open(f'test{i}.txt', 'w'):
        pass

range is used to generate a sequence of integers; it's start inclusive and end exclusive so range(1,11) yields integers 1 to 10. The f string within open provides a nice way to place variables in strings.

Sign up to request clarification or add additional context in comments.

Comments

2

Here's how to do what you want to do in a single line:

for i in range(10): open(f'/tmp/test{i+1}.text', 'w').close()

If you want to create files when they don't exist, but not blow away the contents of any file that already exists, just use a instead of w.

Comments

0

It's simple and easy in python.

#counting starts from 0
for i in range(10):
    i+=1
    with open(f"test{i}.txt","w"):
        pass

F-String in Python

1 Comment

Cleaner would be with open(f"test{i+1}.txt","w"): and get rid of the i+=1 line.
0

Your style is ok, but needs a bit of modification in your code. I corrected your code. Please see below:

import os
def createFiles():
    lab3 = '/home/student/Lab3'
    lab3 = 'F:\ProjectWorks\Python\pythonCore\PythonPractice\MyFolder\MyFolder2'
    # Before creation of files
    dir_list = os.listdir(lab3)
    print("List of directories before creating files")
    print(dir_list)
    print()

    os.chdir(lab3)

    for i in range(1, 11):
        with open(f'test{i}.txt', 'w'):
            pass
    # After creation of files
    dir_list = os.listdir(lab3)
    print("Directory and files after file creation: ")
    print(dir_list)

createFiles()

Comments

-1

What you could do is;

files = 10
for file_name in range(files):
     file = open(‘test’+str(file_name)+'.txt', ‘w’)
     file.close()

This will make 10 blank files

3 Comments

That actually doesn't quite do what was requested.
@Steve What seems to be the issue? I ran it again, and it created files0.txt through to file9.txt. If you want 1 - 10, enter file = open('test'+str(file_name)+'.txt', 'w') instead
At the time I wrote my comment, you weren't adding .txt to the end of the filename. I see that you've since added that. It still isn't technically quite right, but now it's much better.

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.