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
  Commas issue in variable ddahlman 6 455 Apr-05-2024, 03:45 PM
Last Post: deanhystad
Bug argparse io issue pyDream 8 694 Apr-02-2024, 12:42 PM
Last Post: pyDream
  Issue with uninstalling Python that is driving me crazy traxus 0 228 Mar-30-2024, 04:37 PM
Last Post: traxus
  Puzzling import issue that I have no idea how to solvr starseeker 3 524 Feb-21-2024, 05:07 PM
Last Post: deanhystad
  List Comprehension Issue johnywhy 5 544 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  issue in using regex akbarza 4 581 Nov-14-2023, 10:00 AM
Last Post: buran
  openpyxl issue - How to reset sheet.ins_row to the real last row ... ejwjohn 6 793 Nov-08-2023, 01:19 PM
Last Post: ejwjohn
  Facing issue in python regex newline match Shr 6 1,321 Oct-25-2023, 09:42 AM
Last Post: Shr
  [split] Issue installing selenium Akshat_Vashisht 1 550 Oct-18-2023, 02:08 PM
Last Post: Larz60+
  Python C Extension Module loading issue on Cygwin mesibo 0 666 Sep-22-2023, 05:41 AM
Last Post: mesibo

Forum Jump:

User Panel Messages

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