geekchap
geekchap

Reputation: 3

Cmd commands from within python script?

I just want to enter integrate these two commands from cmd and use them in a python script, but I'm novice in both programming and python. How I can achieve this?

cd C:\
python dumpimages.py http://google.com C:\images\

Upvotes: 0

Views: 182

Answers (3)

Li-aung Yip
Li-aung Yip

Reputation: 12486

Use the os.subprocess module.

Note that exec() will also work but it is deprecated in favour of os.subprocess.

Further to my comment above, if all you want to do is retrieve images from a webpage, use GNU wget with the -A flag:

wget -r -P /save/location -A jpeg,jpg,bmp,gif,png http://www.domain.com

Upvotes: 2

ch3ka
ch3ka

Reputation: 12168

I think you are looking for sys.argv

Try:

import sys
print(sys.argv)

on top of your saveimage.py.

It should print something like:

['C:\saveimage.py', 'http://google.com', 'C:\images\']

You see, sys.argv is a list of strings. The first item is the name of the script itself, the others are the parameters. Since lists are 0-based, you can access the i-th parameter to the script with sys.argv[i]

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249562

I suspect the cd C:\ is not necessary, and once that's gone all you seem to be asking for is a way to launch a python script.

Perhaps all you want is to modify saveimage.py so that it has the hard-coded arguments you want? I don't know what that script is--maybe you can tell us if it's important or you aren't allowed to modify it.

Upvotes: 0

Related Questions