Python Forum
Command line argument issue space issue - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Command line argument issue space issue (/thread-38532.html)



Command line argument issue space issue - mg24 - Oct-26-2022

Hi Team,

I am running this script via command line it is working.
python test.py sql_Table1 "\\IND200300400.XXX.XXXX.XXX\Recon Project\output1"

But below code when I try to run multiple py files from single py , code is not working.
it is because of space issue in output folder path ----> "Recon Project"

it is considering Seperate argument. but its single argument of output folder path.
"\\IND200300400.XXX.XXXX.XXX\Recon"
"Project\output1"

from pathlib import Path
import sys
this_dir = Path(__file__).parent
  
for args in [
    "test.py sql_Table1 \\IND200300400.XXX.XXXX.XXX\Recon Project\output1",
    "test.py sql_Table2 \\IND200300400.XXX.XXXX.XXX\Recon Project\output2",
    "test.py sql_Table3 \\IND200300400.XXX.XXXX.XXX\Recon Project\output3"]:


args = args.split()
    filename = this_dir/args[0]
    args[0] = str(filename)
    sys.argv[:] = args
    namespace = {'__name__': '__main__'}
    exec(filename.read_text(), namespace)



RE: Command line argument issue space issue - wavic - Oct-26-2022

Line 11 indentation


RE: Command line argument issue space issue - mg24 - Oct-26-2022

Hi Team,

its a typo error. its easy to fix.

Actual error is command line argument because of space issue.


Thanks
mg


RE: Command line argument issue space issue - deanhystad - Oct-26-2022

Modify args so it works.
for args in [
    ["test.py", "sql_Table1", "\\IND200300400.XXX.XXXX.XXX\Recon Project\output1"],
    ["test.py", "sql_Table2", "\\IND200300400.XXX.XXXX.XXX\Recon Project\output2"],
    ["test.py", "sql_Table3", "\\IND200300400.XXX.XXXX.XXX\Recon Project\output3"]]:
or maybe it should be like this:
for args in [
    'test.py sql_Table1 "\\IND200300400.XXX.XXXX.XXX\Recon Project\output1"',
    'test.py sql_Table2 "\\IND200300400.XXX.XXXX.XXX\Recon Project\output2"',
    'test.py sql_Table3 "\\IND200300400.XXX.XXXX.XXX\Recon Project\output3"']:



RE: Command line argument issue space issue - mg24 - Oct-26-2022

Hi Deanhystad,

I tried above things with single quote, double, and triple quote also no luck.
if there is space in folder path I face issue, last argument. [Recon Project]

I got one more solution from another python. trying to fix.

I am not understanding this line.
A simpler way to accomplish the same thing is to just export a function that takes the filename as an argument and call that function rather than use the exec() thing you are using. Y


A few things are going on here:
Your shell command line is using double quotes around the filename. But your stored command lines are not.
.split() splits by whitespace and doesn’t know anything about quotes.
The built-in shlex.split() will do the Right Thing for you if you use it instead of str.split().
A simpler way to accomplish the same thing is to just export a function that takes the filename as an argument and call that function rather than use the exec() thing you are using.
Reference:

https://docs.python.org/3/library/shlex.html


RE: Command line argument issue space issue - Yoriz - Oct-26-2022

Try it with raw strings by adding an r at the front of each string because the escape character \ might be giving problems
...
for args in [
    r"test.py sql_Table1 \\IND200300400.XXX.XXXX.XXX\Recon Project\output1",
    r"test.py sql_Table2 \\IND200300400.XXX.XXXX.XXX\Recon Project\output2",
    r"test.py sql_Table3 \\IND200300400.XXX.XXXX.XXX\Recon Project\output3"]:
...

Maybe you need to set maxsplit to 2 so the space in Recon Project is ignored
...
args = args.split(maxsplit=2)
...