Search code examples
pythonlinuxcommand-lineargvsys

How do I get input from linux command line into my Python script?


I am trying to execute a command on a file such as chmod in a python script. How can I get the file name from command line to the script? I want to execute the script like so ./addExecute.py blah Where blah is the name of some file. The code I have is this:

#!/usr/bin/python
import sys
import os
file = sys.argv[1]
os.system("chmod 700 file")

I keep getting the error that it cannot access 'file' no such file or directory.


Solution

  • os.system("chmod 700 file")
                         ^^^^--- literal string, looking for a file named "file"
    

    You probably want

    os.system("chmod 700 " + file)
                           ^^^^^^---concatenate your variable named "file"