0

This script loops through a list of url's which download csv files:

#!/usr/bin/python

import subprocess

file = open('links20151111.txt','r')
for url in file:
        print ('[+] downloadin ' + url.strip())
        subprocess.call(['wget', '--content-disposition', url.strip()])

The url's do not contain the file names.

The thing that needs to be done is to replace all "-" with "_" in the file names. A file name could be like this, "traffic_injuries_2001-2014.csv".

Adam
  • 329
  • 1
  • 9
  • 20
  • Your example doesn't replace "all" `-`s with `_`s, just the first two. Can you define that requirement more precisely? – reynoldsnlp Dec 08 '15 at 06:24

2 Answers2

0

If I understand the problem correctly, you could loop through the files in your download directory each time you download a new file and look for the file with a dash in it, then make the character replacement on that file. This should do it:

#!/usr/bin/python
import subprocess, os

def rename_file():
    for f in os.listdir(os.getcwd()): 
        if '-' in f and f.endswith('.csv'): 
            os.rename(f,f.replace('-','_'))

file = open('links20151111.txt','r')
for url in file:
        print ('[+] downloadin ' + url.strip())
        subprocess.call(['wget', '--content-disposition', url.strip()])
        rename_file()

Depending on how the file names are structured you may need to tighten the criteria for your file search. You could compile a regular expression to match the text format more strictly.

John Volk
  • 49
  • 3
  • I solved it using this code: `#!/usr/bin/python import os path = os.getcwd() filenames = os.listdir(path) for filename in filenames: os.rename(os.path.join(path, filename), os.path.join(path, filename.replace('-', '_')))` Thank's for the inspiration. – Adam Dec 08 '15 at 06:40
0

Use the -O option:

wget google.com -O foo.html

More info here.

Otherwise I would suggest using requests module: How to download image using requests

Community
  • 1
  • 1
warvariuc
  • 57,116
  • 41
  • 173
  • 227