tags:

views:

768

answers:

3

Heres my code.

import os, sys
if len(sys.argv) != 2:
     sys.exit(1)

h = os.popen("wget -r %s") % sys.argv[1]
fil = open("links.txt","w")
dir = os.listdir("%s") % sys.argv[1]

for file in dir:
     print file.replace("@","?")
     fil.write("%s/"+file.replace("@","?")) % sys.argv[1]
     fil.write("\n")
h.close()

running the it, like this python project.py http://google.com

gives me error code.

1.py:5 RuntimeWarning: tp_compare didnt return -1 or -2 for exception
h = os.popen("wget -r %s") % sys.argv[1]
Traceback (most recent call last):
File "1.py" line 5, in <module>
h = os.popen("wget -r %s") % sys.argv[1]
TypeError: unsupported operand type<s> for %: 'file' and 'str'

What are im going wrong. (Still learning python) Any solution/ tip?

I dont explain the code, i think you understand what im trying to do

+1  A: 

I think you want:

h = os.popen("wget -r %s" % sys.argv[1])
Ryan Ginstrom
+1  A: 

You're putting the % operator in the wrong place: you need to put it directly after the format string:

h = os.popen("wget -r %s" % sys.argv[1])
...
dir = os.listdir("%s" % sys.argv[1])
...
fil.write(("%s/"+file.replace("@","?")) % sys.argv[1])

Alternatively, since you're just using %s, just do plain and simple string concatenation:

h = os.popen("wget -r " + sys.argv[1])
...
dir = os.listdir(sys.argv[1])
...
fil.write(sys.argv[1] + "/" + file.replace("@","?"))
Adam Rosenfield
+8  A: 
  1. h = os.popen("wget -r %s" % sys.argv[1])

  2. use the subprocess module, os.popen is obsolete

  3. python has urllib, you can consider using that to have pure python code

  4. there is pycurl

Anonymous