Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Errno 22] Invalid argument
#1

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
Reply
#2
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
Reply
#3
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
Reply
#4
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
Reply
#5
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
Reply
#6
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.
Reply
#7
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...
Reply
#8
Issue resolved,
it was a config issue in the PyCharm
Thanks for all the help
Reply
#9
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid argument: 'images\x08ackground.jpg' CatBall 4 905 Jun-19-2023, 09:28 AM
Last Post: CatBall
  [split] [Errno 22] Invalid argument Junaid 0 2,253 Jun-12-2021, 06:02 PM
Last Post: Junaid
  [Errno 22] Invalid argument satyaneel 11 105,779 Jul-14-2020, 08:47 AM
Last Post: lichunming
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,741 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,736 Mar-03-2020, 08:34 AM
Last Post: buran
  Invalid argument error thrown. pyseeker 4 8,568 Sep-10-2019, 07:03 PM
Last Post: pyseeker
  OSError: [Errno 22] Invalid argument - wasn't there last time I ran the code! meganhollie 2 9,128 Jun-11-2018, 06:01 PM
Last Post: meganhollie
  [SOLVED] OSError: [Errno 22] Invalid argument Panda 13 43,626 Jun-04-2018, 08:33 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020