Python Forum

Full Version: [Errno 22] Invalid argument
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Hi,
this code doesn't work for me when running in Pycharm & adding the argument to the run
when trying in CMD - it just ignores the argument
import sys
if str(sys.argv)[1] != "" :
     ....
     ....
else:
    latest_file = sys.argv[1]
Error:
C:\Python27\python.exe: can't open file 'C:/.../Main.py C:/.../report.csv': [Errno 22] Invalid argument
Use something like:
import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--ifile",
                        dest='original_filename',
                        help="Filename where headers are to be replaced",
                        action="store")

    parser.add_argument("-b", "--bfile",
                        dest='replace_original_filename',
                        help="Filename containing body",
                        action="store")

    parser.add_argument("-o", "--ofile",
                        dest='out_filename',
                        help="Output filename",
                        action="store")

    args = parser.parse_args()

    print('original_filename: {}'.format(args.original_filename))
    print('replace_original_filename: {}'.format(args.replace_original_filename))
    print('out_filename: {}'.format(args.out_filename))


if __name__ == '__main__':
    main()
test from command line with:
python TestArgs.py -i 'Myinput1.txt' -b 'MyInput2.txt' -o 'Myoutput.txt'
results:
Output:
λ python TestArgs.py -i 'Myinput1.txt' -b 'MyInput2.txt' -o 'Myoutput.txt' original_filename: 'Myinput1.txt' replace_original_filename: 'MyInput2.txt' out_filename: 'Myoutput.txt'
or with help (-h):
Output:
λ python TestArgs.py -h usage: TestArgs.py [-h] [-i ORIGINAL_FILENAME] [-b REPLACE_ORIGINAL_FILENAME]                    [-o OUT_FILENAME] optional arguments:   -h, --help            show this help message and exit   -i ORIGINAL_FILENAME, --ifile ORIGINAL_FILENAME                         Filename where headers are to be replaced   -b REPLACE_ORIGINAL_FILENAME, --bfile REPLACE_ORIGINAL_FILENAME                         Filename containing body   -o OUT_FILENAME, --ofile OUT_FILENAME                         Output filename
Hi,
thanks for the quick replay.
Doesn't work,
the CSV file is in the same folder of the main.py
import os
import glob
import shutil
import tkMessageBox
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--ifile",
                   dest='original_filename',
                   help="Filename where headers are to be replaced",
                   action="store")
parser.add_argument("-b", "--bfile",
                    dest='replace_original_filename',
                    help="Filename containing body",
                    action="store")

parser.add_argument("-o", "--ofile",
                    dest='out_filename',
                    help="Output filename",
                    action="store")
args = parser.parse_args()
print('original_filename: {}'.format(args.original_filename))
arguments :
C:\Users\asafb\PycharmProjects\Sanity_HB_RR_from_Saved_Pos\Main.py report13122017-103315.csv
Error:
C:\Python27\python.exe "C:/Users/asafb/PycharmProjects/Sanity_HB_RR_from_Saved_Pos/Main.py report13122017-103315.csv" C:\Python27\python.exe: can't open file 'C:/Users/asafb/PycharmProjects/Sanity_HB_RR_from_Saved_Pos/Main.py report13122017-103315.csv': [Errno 2] No such file or directory Process finished with exit code 2
as you run it now, it looks for the csv file in your CWD - C:\Python27\
You need to supply the full path as you do with the Main.py or
run the script from C:/Users/asafb/PycharmProjects/Sanity_HB_RR_from_Saved_Pos/
or add code that extracts the path of the script and add it to the argument to get the full path
Tried it also with full pass
same error:
Error:
C:\Python27\python.exe "C:/Users/asafb/PycharmProjects/Sanity_HB_RR_from_Saved_Pos/Main.py -i C:/Users/Asafb/.../Reports/report13122017-103315.csv" C:\Python27\python.exe: can't open file 'C:/Users/asafb/PycharmProjects/Sanity_HB_RR_from_Saved_Pos/Main.py -i C:/Users/Asafb/.../Reports/report13122017-103315.csv': [Errno 22] Invalid argument Process finished with exit code 2
What I showed you does work. I didn't notice that you were using antique python until now.
try:
c:\python27\python TestArgs.py -i 'Myinput1.txt' -b 'MyInput2.txt' -o 'Myoutput.txt'
Which works , I just tested it.

Also, try this and see what's printed:
python -V
Because if your 'python' does point to python 2.7 the command I first displayed should work as well.
no it is not the same - the previous one is
[Errno 2] No such file or directory

this one is

[Errno 22] Invalid argument

I would guess you have some folder with space in it, i.e. ... masks a folder like Program Files

Also you claimed that csv file is in the same folder as main.py and now you supply different path...
Issue resolved,
it was a config issue in the PyCharm
Thanks for all the help
Always best to run a command line command from the command line not a replicated command line as in PyCharm!
(I've been burnt by that one as well)

Remindes me of a kid's rhyme ... How much wood would a woodchuck chuck if a woodchuck could chuck wood