473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Get the filename from a path (URL) sorted without repeated result

I have a large text file which has a lot of links and I need python script to extract all the names of the files which end with .pdf format and (sorted without repeated result) ?

sample example from the file:

http://www.123.com/file.pdf http://www.123.com/pdfhello
http://www.456.com/hello/one.file.pdf http://www.123.com http://www.123.com
http://www.456.com/hello/one.file.pdf


I need the final result to look like this:

file.pdf
one.file.pdf
Jul 14 '20 #1
2 2007
dev7060
630 Expert 512MB
What have you done so far? Many ways I can think of.

- Store the part of the link after the final '/' in a string and check if the extension is .pdf.
- Store the whole link in a string, start reading from the back and check if that reads fdp.
- Search for ".pdf" in the entire file. If there's an occurrence, keep copying the chars backward to a string until '/' if found (since filenames can't use slash).
etc...

sorted without repeated result
Keep storing the required names in a string array and when it's all done apply the logic of duplicate elements deletion (or you can check if the value is already present in the array before insertion) and then sorting can be done.
Jul 14 '20 #2
SioSio
272 256MB
The only annoyance with this process is that there are multiple URLs on one line.
After reading the entire text, replace the line feed code with a space and split into spaces and store in the array.
After that, write the code as shown by dev7060.
Expand|Select|Wrap|Line Numbers
  1. f = open(r'url.txt','r')
  2. line = f.read()
  3. f.close
  4. url_list = line.replace('\n',' ').split(' ')
  5. url_list.pop(-1)
There are three ways to extract the file name from the URL.
Expand|Select|Wrap|Line Numbers
  1.     list = url.split('/')
  2.     fname = list[len(list)-1]
Expand|Select|Wrap|Line Numbers
  1.     fname = url.rsplit('/',1)[1]
Expand|Select|Wrap|Line Numbers
  1.     fname = url[url.rfind('/')+1:]
Sort(sorted or sort) and duplicate elements deletion(set) use this functions.
Expand|Select|Wrap|Line Numbers
  1. new_file_list = sorted(set(file_list))
Jul 15 '20 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Scott Reynolds | last post by:
Hello! Could someone please provide me a sample, how to extract filename from url? http://www.mydomain.com/eng/Default.aspx -> Default.aspx Thanks Scott
1
by: psbasha | last post by:
Hi, I would like to get the path of the given file name. Say I/P: C:\Sample\Sample1.txt
16
by: RAZZ | last post by:
Hi, When I login to my site initially it load index.php by default. We can't see the page name index.php displayed in URL. But when I click to some link on this page it will display the whole...
6
by: SpreadTooThin | last post by:
After I select a file with the OpenFileDialog, I get the file name with .FileName, but I want to know the drive and path where that file is. What function, method, class can I use in C# to get the...
2
by: =?Utf-8?B?SmFtZXM=?= | last post by:
Hi, I am aware of 256 characters path limit in naming a file, but can you help me how to limit the path for file naming to 240 characters only? is there a way to limit that in registry editor or is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.