2 Replies - 444 Views - Last Post: 21 February 2013 - 06:40 AM Rate Topic: -----

#1 pilotkid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-February 13

how to have python create a file

Posted 20 February 2013 - 06:54 PM

how to have python create a file such as 'neta-inf.txt'. I am trying to self teach the language of python to my self sorry if this is a trivial question
Is This A Good Question/Topic? 0
  • +

Replies To: how to have python create a file

#2 Nekroze  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 170
  • Joined: 08-May 11

Re: how to have python create a file

Posted 21 February 2013 - 02:58 AM

http://docs.python.o...d-writing-files

file = open('neta-inf.txt', 'w')

You could use the 'a' flag instead of 'w' to write to the file without erasing its current contents and still create it if it doesn't exist.

Just look around at the python docs or any of the books and tutorials that are around for free.

Enjoy mate.
Nekroze

This post has been edited by Nekroze: 21 February 2013 - 02:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 lisperati  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-February 13

Re: how to have python create a file

Posted 21 February 2013 - 06:40 AM

Just to go a little more indepth so you can save some stuff in a file and then print it out.


#Open the file for writing to it.

strings = open("neta-inf.txt", 'w')
strings.write("man")
strings.write("\n")
strings.write("hey")
strings.write("\n")
strings.write("radio")
strings.write("\n")

#close the file
strings.close()

#Open the file to read from it.

strings = open("neta-inf.txt", 'r')
for line in strings:
    print line

#always close a file after use to preserve the integrity of the file contents. 
strings.close()



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1