omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Making files with python script

    Pythonista
    file handling pythonista python help python
    4
    6
    5653
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • lukecov
      lukecov last edited by lukecov

      Looking for someone to help me figure out why this doesn't work.

      from sys import argv
      script,filename = argv

      filename = input()

      target.open(filename)

      target.write("hello world this is a random text file.")

      target.close(filename)

      I also have tried adding in 'w' so that it opens it in write mode.

      1 Reply Last reply Reply Quote 0
      • TutorialDoctor
        TutorialDoctor last edited by TutorialDoctor

        from sys import argv
        
        filename=input()
        target=open(filename,'w') #open it for writing, as opposed to reading.
        target.write('hello')
        target.close()
        
        lukecov 1 Reply Last reply Reply Quote 0
        • lukecov
          lukecov @TutorialDoctor last edited by lukecov

          @TutorialDoctor I tried your script, it always tells me no matter what name I put in that it "isn't defined." Also is there any need for importing arguments?

          1 Reply Last reply Reply Quote 0
          • dgelessus
            dgelessus last edited by dgelessus

            @lukecov Are you using Python 2 or 3? There is an important difference with the input function between the two versions.

            In Python 2, there are two functions for reading user input, called input and raw_input. The raw_input function gives back the text typed by the user, as you would expect. The input function executes the user input as Python code (similar to how the >>> prompt works) and returns the result. Most likely you are using Python 2, which means the input function tries to run your filename as Python code. Because it's a single word, it looks like a variable name, and Python complains that the name isn't defined.

            In Python 2, you should never, ever use the input function. Not only is it confusing to users (the user can't tell if input or raw_input is used), but it is also a security problem. The user can type any Python code in there, for example code that deletes everything in Pythonista's Documents folder. You're just asking for a file name, so raw_input is the function to use here.

            Because of how bad and useless the Python 2 input function is, it was removed in Python 3. Instead, Python 2's raw_input was renamed to input in Python 3 (and there is no raw_input in Python 3). This means that under Python 3 input is the correct way to ask for text from the user.

            If you're unsure what Python version your program is running on, you can add this code at the top to print out the Python version info:

            import sys
            print(sys.version)
            

            To answer your other question - no, there is no need to import argv in the script posted by @TutorialDoctor. I'm guessing they forgot to remove the import after modifying your original code. Of course you can also change your code to take the filename from sys.argv, but in Pythonista this not as useful as on a normal system, because Pythonista has no shell. By default Pythonista passes no arguments when running a program, but if you want, you can long-press the run button to pass sys.argv arguments to your program.

            lukecov 1 Reply Last reply Reply Quote 2
            • ccc
              ccc last edited by

              I think the key difference is the syntax target.open(filename) vs. target = open(filename).

              1 Reply Last reply Reply Quote 0
              • lukecov
                lukecov @dgelessus last edited by

                @dgelessus thanks that helps a lot, and makes a lot sense. I'll make sure to not use input() on version 2. Even though I was just making files to mess around with not for any real purpose. Thanks a lot for explaining it.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors