Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
Can anyone tell me how to create a file in Python?

1 Answer

0 votes
by (108k points)

In Python, you can create a text file with the help of the below code:

f= open("sample.txt","w+")

If you want to add numbers from 1 to 10 in this sample.txt file, then you can refer to the below code:

for i in range(10):

     f.write("This is line %d\r\n" % (i+1))

After writing the text in the file, now, you can close the file with the help of bellow code:

f.close() 

If you are looking for an online course to learn Python, I recommend this Python Course by Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 10, 2019 in Python by Sammy (47.6k points)

Browse Categories

...