Python Forum
Command line argument issue space issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Command line argument issue space issue
#1
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)
Reply
#2
Line 11 indentation
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Hi Team,

its a typo error. its easy to fix.

Actual error is command line argument because of space issue.


Thanks
mg
Reply
#4
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"']:
Reply
#5
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
Reply
#6
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)
...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  White Screen Issue with Toolbar After Python Installation evelynfreya 3 1,215 Apr-30-2025, 06:43 AM
Last Post: encore
  Selenium Select Option Issue webimdad 0 487 Apr-23-2025, 12:26 PM
Last Post: webimdad
  Insert command line in script lif 4 1,039 Mar-24-2025, 10:30 PM
Last Post: lif
  I have created a bot in python but getting issue in element selection domic33 0 475 Mar-16-2025, 01:55 PM
Last Post: domic33
  issue with multiprocessing in embeded python otaolafr 1 342 Mar-05-2025, 06:24 PM
Last Post: deanhystad
  app funcionality issue jacksfrustration 4 939 Feb-05-2025, 06:47 PM
Last Post: jacksfrustration
  Bright Black Screen Issue in Tkinter GUI Application rommy 2 1,113 Nov-29-2024, 10:50 PM
Last Post: woooee
  python3.9 auto_ossec issue jwdvd 2 863 Nov-18-2024, 09:56 AM
Last Post: jwdvd
Photo the python using issue,please help to check what is wrong with it Beginner_ 1 769 Nov-13-2024, 06:53 PM
Last Post: deanhystad
  Python install issue redreign83 2 874 Oct-04-2024, 07:59 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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