0

I've written the below

url='http://lle.gov.wales/catalogue/item/TraditionalOrchards.zip'
wget.download(url, '/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/TraditionalOrchards.zip')

file_name = "/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/TraditionalOrchards.zip"

with ZipFile(file_name, 'r') as zip:
    zip.printdir()
    zip.extractall('/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/Unzipped')

old_name = r"C:/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/Unzipped/TRADITIONAL_ORCHARDSPolygon.shp"
new_name = r"C:/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/Unzipped/traditional_orchardspolygon.shp"
os.rename(old_name, new_name)

old_name = r"C:/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/Unzipped/TRADITIONAL_ORCHARDSPolygon.dbf"
new_name = r"C:/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/Unzipped/traditional_orchardspolygon.dbf"
os.rename(old_name, new_name)

old_name = r"C:/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/Unzipped/TRADITIONAL_ORCHARDSPolygon.prj"
new_name = r"C:/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/Unzipped/traditional_orchardspolygon.prj"
os.rename(old_name, new_name)

old_name = r"C:/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/Unzipped/TRADITIONAL_ORCHARDSPolygon.shx"
new_name = r"C:/Users/DylanContet/Desktop/AutomationProject/LayerListFiles/60/Unzipped/traditional_orchardspolygon.shx"
os.rename(old_name, new_name)

as a way to download a zipfile, unzip it and rename the specific files (like .shp, .prj, etc.).

Is there a way to use Python to read a txt file to open a link in that txt file, then to use a specified name on the txt file to rename all the files in that ZIP?

So for example it will look like the following in the .txt

url_to_website   new_name_for_files
url_to_website_2   new_name_for_files_2

I'd like it to go line by line and download then with the url then rename with the new name next to the URL.

I can do it 1 file at a time as shown above but I've got over 300 files to download and rename.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • sorry for the bad formatting, I just copied and pasted the code –  Dec 13 '21 at 13:17
  • hey @Dylan, pls format your question so the folks in SO can help you way better. Also a minimal example should be enough, happy coding :) – y.y Dec 13 '21 at 13:18
  • The only difference I see is that the new names are lower case, is that correct? – 001 Dec 13 '21 at 13:20
  • Also, for future reference: https://stackoverflow.com/editing-help – 001 Dec 13 '21 at 13:21
  • unfortunately not, the name of the file might be just an abbreviation of all words used. –  Dec 13 '21 at 13:23
  • You can loop through all the files with: [How do I list all files of a directory?](https://stackoverflow.com/a/3207973). Then you can process each name with [`str.replace()`](https://docs.python.org/3/library/stdtypes.html#str.replace) or [regex](https://docs.python.org/3/library/re.html). – 001 Dec 13 '21 at 13:26
  • thank you will give this a look, also do you know if python is able to read a url written in a txt file then open it? –  Dec 13 '21 at 13:32
  • Open it in a web browser or open it as html text? [How can I open a website in my web browser using Python?](https://stackoverflow.com/q/31715119) – 001 Dec 13 '21 at 13:38
  • apologies, to open the url written in a .txt in a browswe –  Dec 13 '21 at 13:39
  • Open the text file, read it to get the URL, then open the website. If there is more than just a single URL in the file, then nobody can help without seeing the file contents. – Peter Dec 13 '21 at 13:54

0 Answers0