0

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

3 Answers 3

9
  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

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

Comments

1

I think you want:

h = os.popen("wget -r %s" % sys.argv[1])

Comments

1

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("@","?"))

Comments

Your Answer

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