-1

I need one help. I need to download the file from remote source and storing it into local folder using Python. I am explaining my code below.

def downloadfile(request):
    """ This function helps to download the file from remote site"""

    if request.method == 'POST':
        URL = request.POST.get('file') #i.e-http://koolfeedback.com/beta/about-us.php
        filename = "status"
        with open(filename,'wb') as fyl:
            fyl.write(urllib2.urlopen(URL).read())
            fyl.close()

Here I need to download the page and store into the local download folder using zip format.Please help me.

  • What is `request` here means? – Rahul Sep 12 '17 at 10:25
  • I have already mention the post data. `request.POST.get('file')=http://koolfeedback.com/beta/about-us.php`. This URL is coming from form parameter and i need to download that exact page and store it into local folder. –  Sep 12 '17 at 10:28
  • Looks like you are already saving the content from that URL in a file named "status". Do you want to compact this file using DEFLATE compression and move it to a folder named download? – Paulo Scardine Sep 12 '17 at 10:29
  • yes i need to move it into download folder which is present inside the project directory. –  Sep 12 '17 at 10:30
  • you are doing `request.POST.get...` but from where the `request` object comes? are you using django? – Rahul Sep 12 '17 at 10:42
  • Yes, I am using Django. –  Sep 12 '17 at 10:45

2 Answers2

1

You likely want to use the urllib function urlretrieve rather than urlopen, which is for opening remote files (such as a text file on a remote server you what to read text from, not files you want to download).

See also: https://stackoverflow.com/a/22682/6328995

Tyler
  • 106
  • 1
  • 5
  • No, My main aim is to include the remote file. –  Sep 12 '17 at 10:38
  • then just link it? – Jingo Sep 12 '17 at 10:49
  • have you tried: `fyl.write((urllib2.urlopen(URL)).read())` ? the nesting of the functions may be causing the issue. Also, try replacing this line with `print(urllib2.urlopen(URL)).read())` to see if it is actually reading the file properly – Tyler Sep 12 '17 at 10:54
0

If you are using django something like this;

folder_to_store = "path/to/folder"
full_filename = os.path.join(folder_to_store, request.FILES['file'].name)
fout = open(full_filename, 'wb+')
for chunk in fout.chunks():
    fout.write(chunk)
fout.close()
Rahul
  • 10,830
  • 4
  • 53
  • 88