-4

I need to know the code to create a notepad file straight from Python.

I have been searching for it but I cant find it. My teacher said that it should create a file automatically. I tried to use:

filename=raw_input("")
target = open (filename, 'a')

It works just fine if I don't put it into a while loop or an if statement. As soon as I do though the whole thing blows.

I am trying to create an option menu to chose between reading or writing.

def main():
   foo = open("filename.txt", "w")
   foo.write('blah-blah-blah')
   foo.close()
   main()

   if option == '2':
      def main2():
         foo = open("filename.txt", "r")
         print(foo.read())
         foo.close()
         main2()
   keepgoing=input("Enter another statement?(y or n):")
   again=input("Run simulation again?(y or n): ")
   print()

   if again!= "y":
       again=False
tzrm
  • 513
  • 1
  • 8
  • 14
user3059576
  • 1
  • 1
  • 3
  • 4
    I think http://stackoverflow.com/questions/12654772/create-empty-file-using-python answers your question already. A "Notepad file" is just an empty file whose name happens to end in ".txt". – CmdrMoozy Dec 04 '13 at 23:50
  • FYI, "I need the code" and "I can't figure it out" won't draw much sympathy here. Simply ask your question, ie "Why isn't my python script creating a file? – Brad Koch Dec 05 '13 at 00:00
  • Im not asking for sympathy. I'm asking for help. Besides i figured it out by myself. – user3059576 Dec 05 '13 at 00:22
  • @user3059576 not according to that code you didn't, that won't create a file – Peter Gibson Dec 05 '13 at 00:35
  • Sorry about the code block mistakes. not sure how to put them in there yet. – user3059576 Dec 05 '13 at 01:40

1 Answers1

2

You can create plain text file this way:

foo = open("filename.txt", "w")
foo.write('blah-blah-blah')
foo.close()
gthacoder
  • 2,213
  • 3
  • 15
  • 17