-2

I was searching the web and never found anything, but I was probably just searching wrong. My goal of this fun meme project is to create a script that makes a file that contains numbers that count up to a certain specific amount on separate lines. So far I have:

import os

os.chdir("path")

myFile = open("numbers.txt", "wt")


x = 1
for i in range(10):
    x += 1
print(x)



myFile.close()

myFile = open("numbers.txt", "r")
print(myFile.read())

My question is how to write the numbers on separate lines.

Sorry if there is some confusion I'm new to python and a young programmer and this is probably an easy fix. Thanks in advance :P

  • 1
    You should first work through the [Python tutorial](https://docs.python.org/3/tutorial/) if not done yet. – Michael Butscher Oct 31 '20 at 15:34
  • Thanks for the Python tutorial I will certainly look through that. I know how to use the \n stuff, but I don't know how make that work with the setup I currently have. Thanks though. – yourfriendlyprogrammer Oct 31 '20 at 15:37
  • 1
    [https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) ... myfile.write(....) – Patrick Artner Oct 31 '20 at 15:38

1 Answers1

2

Researching a bit about file handlers, escape strings and for loops would be useful:

with open("numbers.txt", "wt") as f:
    x = 1
    for i in range(10):
        x += 1
        f.write(str(x)+'\n')
Hamza
  • 5,373
  • 3
  • 28
  • 43