I want to read an array from a file, and add each word in the array to the user input. Imagine that the user input is wwww.google.com/
the application adds each word in the array behind the link until the list is finished.
google.com/
google.com/test.html
google.com/test1.html
I can do this if I make an array in the program and it works fine but when I want to read from a file, instead of reading the whole word it only reads letter by letter.
google.com/
google.com/t
google.com/e
google.com/s
google.com/t
and continue like this. How can I make it read the whole word when it reads the file instead of letter by letter.
Here is my code if you need it to reference an example of the array:
['test/','test1/','test2/']
website = raw_input(Fore.MAGENTA +"> SITIO PARA SCANEAR: ")
if "http://" not in website:
website ="http://"+ website
with open("test.txt", "r") as ins:
array=[]
for line in ins:
print line
for i in line:
try:
adminpanel = urllib2.urlopen(website+i)
checkurl = adminpanel.code
if checkurl == 200:
print Fore.GREEN+ website+i," "" [+] Encontrado [+]"
continue
else:
print Fore.RED+ website+i," "" [-]Error[-]"
From your comments I understand that you need to iterate over the list that is as text in your file. You can make use of
eval()
:eval
is a source of insecurity and it is recommended not to use it except if you are the one in control of the whole process and you know that test.txt is safe...If you don't want to use it
eval
, you have several options:You can save the urls in a more standard way so that you can read the file more 'correctly'.
You can use the function
ast.literal_eval
inside the moduleast
.You can use the following function to parse the text present in test.txt and convert it to a list.
Function to use:
That function works for the example you have put,
['test/','test1/','test2/']
but it can give errors if that example changes. What the function does is remove the brackets and quotes and convert the final result to a list of strings separating everything between commas.If you want to integrate it into the code that @Gocht has left you, you can do it like this:
In my opinion, the best option would be the first one but it forces you to modify the code that test.txt generates .