Test if file downloaded from url python






















Avi Aryan Follow. Published Apr 17, Getting filename from URL We can parse the url to get the filename. Python Requests Http File download Scripts. I am a freelance developer currently working at Toptal and Udacity. I expertise in full stack web development.

I have been programming for 6 years and I believe in code sanity as much as anything. I also do top-level competitive p Discover and read more posts from Avi Aryan. Be the first to share your opinion. GitHub flavored markdown supported. Elias Rodrigues. Just awesome! Thanks Man!!! Ricardo Wilhelm. Download ZIP. Python function to test if a file at a URL exists. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below.

To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters. This comment has been minimized. Sign in to view. Copy link Quote reply. I rewrote this function for Python 3: import urllib. Request url request. Since the Content-Disposition header is not always available, you could use a solution that not only looks for that specific header, but also looks at the individual file types within the Content-Type header.

Here is a list of Content-Types. The code below checks the headers for Content-Disposition , but it also checks the headers for some of the Content-Type that are commonly downloadable. I also added a check for the Content-Length, because it could be useful in chunking the file being downloaded.

Content-Disposition provides filename information if it is not given in the url. But this information is not always present, as is the case with your url.

A solution is to filter by content type, see the example below. I think your former code works but with a slight modification. It's trying to download the complete file due to which it's getting hanged every time you run. This is not explained in user documentation. But by a guess we can say , chunked transfer encoding is being done, the data stream is divided into a series of non-overlapping "chunks". The chunks are sent out independently of one another by the server.

You could check the content-type response header. This header defines the media type of the requested resource. The most common types are shown here. The parameter value is not mandatory but type and subtype are. There are currently 7 types as defined by RFC :. The header you are looking for varies with the resource you are expecting, but some examples you may use.

It is worth noting the types are fixed, you cannot define a new subtype but you can define a new subtype. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.



0コメント

  • 1000 / 1000