-1

I am new to python. As a part of my project, I am trying to create a linux command for the python file which I have already. For example I have a python file example.py,

with open('file.txt','r') as f:
    for i in f:
        print i
with open('file.txt','r') as f:     #for different reasons I am opening file again
    for i in f:
        print i

Here I am trying to make a command like $example --print file.txt . Which means I am giving the input file from the command itself. Then it has to print the content of the input file. Please help me how to achieve this one. Thanks in advance.

sowji
  • 103
  • 10
  • Possible duplicate of [What do I use on linux to make a python program executable](http://stackoverflow.com/questions/304883/what-do-i-use-on-linux-to-make-a-python-program-executable) – tripleee Apr 11 '17 at 10:43

2 Answers2

2

You need to add the interpreter to the head of your script and also use the sys.env to access the input parameters:

#!/usr/bin/python

import sys, os;

# check so we have enough arguments
if len(sys.argv) < 2:
  printf "You need to supply at least two arguments."
  exit(-1)

# check so the file exists
if os.path.exists(sys.argv[1]):
  # try to open the filename supplied in the second argument
  # the sys.argv[0] always points to the name of the script
  with open(sys.argv[1], 'r') as f:
    for i in f:
      print i
else:
  print 'The file %s does not exists' %(sys.argv[1])

Notice that you might need to change the /usr/bin/python to the path where your python binary is installed. You can find out where it is by issuing the following command:

whereis python

Now you should be able to run your command like this:

./command file.txt

Notice, that here I assume that you are standing in the same folder as the python script. You could also move the script into a folder which is in your $PATH environment variable so you could access it from everywhere. For instance you could move it to /usr/local/bin:

mv command /usr/local/bin/.

You should then be able to run it from any folder using:

command file.txt

The last thing you need to do is to make sure the script is executable:

chmod +x command
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
  • Don't forget to make the script executable: `chmod +x command`. – Sven Rusch Apr 11 '17 at 10:37
  • @SvenFestersen - I intended to include a chmod command from the beginning but I forgot =) – Cyclonecode Apr 11 '17 at 10:39
  • Thanks for the answer @Cyclonecode . Can you tell me how to give multiple files as input in the command. – sowji Apr 11 '17 at 10:54
  • @sowji - Would appreciate an upvote =) You only need to check another index in the `sys.argv[]` list i.e `./command fileA.txt fileB.txt` then in your script `if os.path.exists(sys.env[1]):` and then check for the second file `if os.path.exists(sys.env[2]):` – Cyclonecode Apr 11 '17 at 11:30
0

You need the following:

  1. The beginning of your script should have #!/usr/bin/env python
  2. sys.argv is a list containing your command line arguments
ffff
  • 2,853
  • 1
  • 25
  • 44