I'm trying to create a program that lets the user type in anything they want and that stores everything that has ever been typed into it (i.e. it doesn't disappear when the program closes). I've tried looking online for tutorials on how to work with files in python but those tutorials never seem to mention how to create a file. Anyone know how?
How do you create a file in python?
Page 1 of 19 Replies - 571 Views - Last Post: 08 November 2011 - 05:30 PM
Replies To: How do you create a file in python?
#2
Re: How do you create a file in python?
Posted 07 November 2011 - 03:12 PM
#3
Re: How do you create a file in python?
Posted 07 November 2011 - 04:04 PM
Yes, but I need to know that my files will continue to exist and store the data after I close the program.
#4
Re: How do you create a file in python?
Posted 07 November 2011 - 04:16 PM
In the same directory as your program, a file will appear called "name.txt" if you execute this code in your program:
Continue to write "lines" in your program to the text document with:
When you have finished use:
The file will continue to exist and store the data in the same directory you ran your Python program from.
file = open("name.txt", "w")
Continue to write "lines" in your program to the text document with:
file.write(line)
When you have finished use:
file.close()
The file will continue to exist and store the data in the same directory you ran your Python program from.
This post has been edited by Simown: 07 November 2011 - 04:17 PM
#5
Re: How do you create a file in python?
Posted 07 November 2011 - 05:20 PM
That's not working for me.
No matter how many times I run the following code
It always outputs "Hello World Hello World"
No matter how many times I run the following code
filename = "name.txt"
FILE=open("name.txt", 'w')
FILE.write("Hello World ")
FILE.write("Hello World")
FILE.close()
FILE=open("name.txt",'r')
print FILE.readlines()
FILE.close()
It always outputs "Hello World Hello World"
#6
Re: How do you create a file in python?
Posted 07 November 2011 - 05:27 PM
That's what you wrote to the file though, so that's what it's going to say. What did you expect/want it to say?
This post has been edited by Simown: 07 November 2011 - 05:28 PM
#7
Re: How do you create a file in python?
Posted 07 November 2011 - 05:30 PM
The second time I run the program, I need it to print "Hello World Hello World" twice and on the third time I need it to print "Hello World Hello World" three times. What do I need to do to get it to save the info I store in it?
#8
Re: How do you create a file in python?
Posted 07 November 2011 - 05:44 PM
In that case you need to open the file in "append" mode to add to the end of the file:
And then just write items and close as usual.
file = open("name.txt", "a")
And then just write items and close as usual.
#9
Re: How do you create a file in python?
Posted 07 November 2011 - 05:46 PM
Thanks
Will append work if you haven't added anything in the file yet?
Will append work if you haven't added anything in the file yet?
This post has been edited by sniderj1: 07 November 2011 - 05:51 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|