1

I need to do something like this, but in Python instead of Bash:

i=1
while [ $i <= 10 ] ; do
    wget http://somewebsite.net/shared/fshared_$i.7z
    $i = $i + 1
done

In Python I tried with the following:

import urllib, os
i = 0
while i <= 3:
    os.system('wget http://somewebsite.net/shared/fshared_',i,'.7z')
    i = i + 1

But it does not work, the variable is not concatenated correctly (or something similar).
Using Bash code does not work, apparently in Bash can't do something simple like: i = i + 1

Could anyone help me with this?

SOLVED! :)

Now I have the script both Bash and Python, actually with Python I have several variants.

Thanks to all... thanks a lot ^-^

How do I mark the topic as solved?

Thanks again.

1
  • If one of the answers solved your problem, you accept it. If there is more than one correct answer, accept the one you consider best. Commented May 30, 2013 at 20:22

6 Answers 6

3

Try

os.system('wget http://somewebsite.net/shared/fshared_%s.7z'%i)

use %s instead of ,

Sign up to request clarification or add additional context in comments.

2 Comments

Using Python v2: TypeError: cannot concatenate 'str' and 'int' objects Using Python v3: TypeError: Can't convert 'int' object to str implicitly And I already put before the downlaod line: str(i)
you forget to remove '+' and ',' stuff
1

You can increment in Bash. You have to do something like:

i=3
(( i++ ))
echo $i

That last like should print 4. So your script would be:

i=1
while [ $i -le 10 ] ; do
    wget http://somewebsite.net/shared/fshared_$i.7z
    (( i++ ))
done

Edit: fixed code to use -le instead of <= since <= doesn't work in bash.

Comments

1

You can use python without calling out to the shell for each file using urllib

import urllib, os

for i in range(4):
    filename = 'fshared_{}.7z'.format(i)
    urllib.urlretrieve('http://somewebsite.net/shared/'+filename, filename)

1 Comment

I had tried before with urllib (i don't like mixing bash and python) but it did not work, but with your code it works perfectly, thanks.
1

To fix the problem with the concatenation, you need to use + to concatenate instead of ,, because when you use , to concatenate two strings it separate them with a space character between, the same doesn't happen when you use + because it "really" concatenate the strings.

E.g.:

print('Hello' + 'World' + '!')
print('Hello', 'Stack', 'Overflow!')

Output:

HelloWorld!
Hello Stack Overflow!

Your code would look like this now using + instead of ,:

#...
os.system('wget http://somewebsite.net/shared/fshared_' + i + '.7z')
#...                                                    ^   ^

Code pasted on Ideone.com

Comments

0

Try this

import urllib, os
i = 0
while i <= 3:
    os.system('wget http://somewebsite.net/shared/fshared_%s.7z' % i)
    i = i + 1

Comments

0

you can also specify python to ask you the url to pass to wget

import os
path = raw_input("enter the url:")
os.system('wget -r -nd -l1 --no-parent -A mp3 %s'%path)

in place of 'mp3' use whatever format you want to download.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.