1

I am taking path as the input in command line , but it is not taking input from the other directory, my code is this

filename1 = os.path.basename(sys.argv[1])
filename2 = os.path.basename(sys.argv[2])
filename3 = os.path.basename(sys.argv[3])
wk1 = openpyxl.load_workbook (filename1)
wk2 = openpyxl.load_workbook (filename2)

and passing command line arguments like this,

python Final.py "C://Users//Folder One//Folder Two//Input1.xlsx" 
"C://Users//Folder One//Folder Three//Input2.xlsx" "C://Users//Folder 
One//Folder Four/Output.xlsx"

FileNotFoundError: [Errno 2] No such file or directory: 'Input1.xlsx'

I need to take input 2 files and store the output in third file i.e Output.xlsx. 3rd argument decides the path for the output file.

6
  • If you only take the filename (not the full path) then how would that work without first setting the working/current directory before calling load_workbook ? You pass the full path, so why only use the filename? Commented Sep 26, 2018 at 6:19
  • @Tim Williams I am getting error in command line arguments i.e File Not found for output file it is working fine but for the input file it is not working Commented Sep 26, 2018 at 6:30
  • You are using double quotes twice for the 2nd argument. Why is it so? Commented Sep 26, 2018 at 7:33
  • It is just writing mistake here @shahkalpesh Commented Sep 26, 2018 at 7:39
  • Please include the error message. Commented Sep 26, 2018 at 8:00

1 Answer 1

1

if you're trying to read from another directory then maybe you should pass the filepath directly to the load_workbook function instead.

The os.path.basename function returns the name of the file Input1.xlsx. Since you invoked the script from a different location, it will fail to detect the file from the source directory. Right now your script attempts to to look for your input files from the same directory as your Final.py, or wherever else that you've navigated to prior to attempting to load the workbooks.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this, wk1 = openpyxl.load_workbook (argv[1]) this is not working @kerwei
argv[1] or sys.argv[1]?

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.