9 Replies - 955 Views - Last Post: 08 November 2011 - 05:30 PM Rate Topic: -----

#1 sniderj1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 17-July 10

How do you create a file in python?

Posted 07 November 2011 - 03:09 PM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: How do you create a file in python?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9389
  • View blog
  • Posts: 35,251
  • Joined: 12-June 08

Re: How do you create a file in python?

Posted 07 November 2011 - 03:12 PM

You mean basic file io with python? Like this?

http://www.penzilla..../python/fileio/
Was This Post Helpful? 0
  • +
  • -

#3 sniderj1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 17-July 10

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.
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

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:

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

Was This Post Helpful? 0
  • +
  • -

#5 sniderj1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 17-July 10

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
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"
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

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

Was This Post Helpful? 0
  • +
  • -

#7 sniderj1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 17-July 10

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?
Was This Post Helpful? 0
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

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:

file = open("name.txt", "a")


And then just write items and close as usual.
Was This Post Helpful? 1
  • +
  • -

#9 sniderj1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 17-July 10

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?

This post has been edited by sniderj1: 07 November 2011 - 05:51 PM

Was This Post Helpful? 0
  • +
  • -

#10 Vblaster  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 16-January 11

Re: How do you create a file in python?

Posted 08 November 2011 - 05:30 PM

Yes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1