1

I have a python file that isn't working.

I would like to run this script on it:

with open('beak', 'rb+') as f:
    content = f.read()
    f.seek(0)
    f.write(content.replace(b'\r', b''))
    f.truncate()

Source

I don't know how to make multiple lines on command line and I am not really sure how to execute my code. Do I just put my file name in place of 'beak' and do I just cd to the folder my file is in before I execute this script?

user8233898
  • 43
  • 1
  • 8

1 Answers1

1

You can type that into the Python command line. Type the first line and return, it will recognize that you're in the middle of a with clause and allow you to type the remaining lines one at a time (be sure to get the indentation right). After the last line, return twice and it will execute.

This script assumes that you are going to read a file named "beak". You need to run this script from the same directory where "beak" is. ("beak" should really have an extension, like ".txt", depending on what kind of file it is).

Doing long scripts from the command line like this is not the best way -- it's better to put this code in a file ("reader.py", for example -- and put reader.py in the same directory as "beak"). Then you can simply execute by typing "python reader.py".

cag51
  • 178
  • 4
  • 14
  • I figured it should work just by typing in the first line and hitting error but I received this message: -sh: syntax error near unexpected token `('. I will try running it from a file – user8233898 Jul 26 '17 at 18:45
  • Looks like you are running from Terminal. Terminal is not a Python interpreter; it is a bash interpreter. From terminal, type "python" to open a python interpreter, then you can run Python code. I suggest following a tutorial to guide you through these introductory steps, see https://learnpythonthehardway.org/book/ex0.html – cag51 Jul 27 '17 at 16:29