Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43667 > unrolled thread
| Started by | PEnergy <prquinn@gmail.com> |
|---|---|
| First post | 2013-04-16 07:14 -0700 |
| Last post | 2013-04-16 13:25 -0600 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
Calling python script in dos and passing arguments PEnergy <prquinn@gmail.com> - 2013-04-16 07:14 -0700
Re: Calling python script in dos and passing arguments Chris Rebert <clp2@rebertia.com> - 2013-04-16 12:10 -0700
Re: Calling python script in dos and passing arguments Alister <alister.ware@ntlworld.com> - 2013-04-16 19:19 +0000
Re: Calling python script in dos and passing arguments Tim Roberts <timr@probo.com> - 2013-04-16 22:32 -0700
Re: Calling python script in dos and passing arguments Michael Torrie <torriem@gmail.com> - 2013-04-16 13:25 -0600
| From | PEnergy <prquinn@gmail.com> |
|---|---|
| Date | 2013-04-16 07:14 -0700 |
| Subject | Calling python script in dos and passing arguments |
| Message-ID | <88b87ac0-1fce-4383-9841-d99a49f50556@googlegroups.com> |
Greetings, I am trying to write a python script that, when called from the DOS prompt, will call another python script and pass it input variables. My current code will open the other python script but doesn't seem to pass it any values: import os,sys,subprocess subprocess.Popen(['python.exe','C:\NDEX\GRE2\uip\uip_20.py','t3c*']) Am I missing something or is this type of call not possible through DOS? Thanks, PEnergy
[toc] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2013-04-16 12:10 -0700 |
| Message-ID | <mailman.688.1366139419.3114.python-list@python.org> |
| In reply to | #43667 |
On Tue, Apr 16, 2013 at 7:14 AM, PEnergy <prquinn@gmail.com> wrote:
> Greetings,
>
> I am trying to write a python script that, when called from the DOS prompt, will call another python script and pass it input variables. My current code will open the other python script but doesn't seem to pass it any values:
>
> import os,sys,subprocess
> subprocess.Popen(['python.exe','C:\NDEX\GRE2\uip\uip_20.py','t3c*'])
>
> Am I missing something or is this type of call not possible through DOS?
1. Backslash is an escape character in Python strings (e.g. "\n" =
newline). You should therefore double-up on your backslashes. (Your
exact string just so happens to work due to a misfeature regarding how
invalid backslash escapes are handled.)
2. Glob/wildcard ("*") expansion is done by the shell, but
subprocess.Popen does not use the shell by default (for good reason!).
Use the `glob` library to do the expansion yourself, in Python:
http://docs.python.org/2/library/glob.html
Cheers,
Chris
[toc] | [prev] | [next] | [standalone]
| From | Alister <alister.ware@ntlworld.com> |
|---|---|
| Date | 2013-04-16 19:19 +0000 |
| Message-ID | <Yuhbt.12331$DQ3.8267@fx27.fr7> |
| In reply to | #43701 |
On Tue, 16 Apr 2013 12:10:09 -0700, Chris Rebert wrote:
> On Tue, Apr 16, 2013 at 7:14 AM, PEnergy <prquinn@gmail.com> wrote:
>> Greetings,
>>
>> I am trying to write a python script that, when called from the DOS
>> prompt, will call another python script and pass it input variables.
>> My current code will open the other python script but doesn't seem to
>> pass it any values:
>>
>> import os,sys,subprocess
>> subprocess.Popen(['python.exe','C:\NDEX\GRE2\uip\uip_20.py','t3c*'])
>>
>> Am I missing something or is this type of call not possible through
>> DOS?
>
> 1. Backslash is an escape character in Python strings (e.g. "\n" =
> newline). You should therefore double-up on your backslashes. (Your
> exact string just so happens to work due to a misfeature regarding how
> invalid backslash escapes are handled.)
> 2. Glob/wildcard ("*") expansion is done by the shell, but
> subprocess.Popen does not use the shell by default (for good reason!).
> Use the `glob` library to do the expansion yourself, in Python:
> http://docs.python.org/2/library/glob.html
>
> Cheers,
> Chris
why cant you just import the 2nd python program & then call its main
function (or any other) with the required parameters?
--
You are wise, witty, and wonderful, but you spend too much time reading
this sort of trash.
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-04-16 22:32 -0700 |
| Message-ID | <fscsm8li4joshifqaoemgu5b8d9onh57qu@4ax.com> |
| In reply to | #43701 |
Chris Rebert <clp2@rebertia.com> wrote:
>
>2. Glob/wildcard ("*") expansion is done by the shell, but
>subprocess.Popen does not use the shell by default (for good reason!).
This is only true in Linux. In Windows, the wildcard characters are passed
to the program, so each app must do its own glob expansion.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2013-04-16 13:25 -0600 |
| Message-ID | <mailman.689.1366140333.3114.python-list@python.org> |
| In reply to | #43667 |
On 04/16/2013 08:14 AM, PEnergy wrote:
> Greetings,
>
> I am trying to write a python script that, when called from the DOS
> prompt, will call another python script and pass it input variables.
> My current code will open the other python script but doesn't seem to
> pass it any values:
>
> import os,sys,subprocess
> subprocess.Popen(['python.exe','C:\NDEX\GRE2\uip\uip_20.py','t3c*'])
I find it easier just to write my python programs such that they can be
imported into other python scripts. Usually I use this form:
def func(*whatever):
pass
if __name__ == "__main__":
# parse command-line arguments
# call functions in this module with those args
func(1,2,3,etc)
This way a script can be run standalone, or I can import it and access
its attributes direction. I recommend you consider this approach.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web