-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
5
  • 4
    I think stackoverflow.com/questions/12654772/… answers your question already. A "Notepad file" is just an empty file whose name happens to end in ".txt". Commented Dec 4, 2013 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? Commented Dec 5, 2013 at 0:00
  • Im not asking for sympathy. I'm asking for help. Besides i figured it out by myself. Commented Dec 5, 2013 at 0:22
  • @user3059576 not according to that code you didn't, that won't create a file Commented Dec 5, 2013 at 0:35
  • Sorry about the code block mistakes. not sure how to put them in there yet. Commented Dec 5, 2013 at 1:40

1 Answer 1

2

You can create plain text file this way:

foo = open("filename.txt", "w")
foo.write('blah-blah-blah')
foo.close()
Sign up to request clarification or add additional context in comments.

1 Comment

Might want to add a newline at the end of the string written.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.