unnamed.gh (11.6 KB)
I’ve been working with a Python script for the past few days.
My goal is to find and read a file from within a rar file and save it to my drive for future use. However, I’m running into an issue where it seems I don’t have the necessary permissions to access this process.
It either grants access or show below error.
PermissionError: [WinError 5] Access is denied
or
Error running script: Cannot find working tool [28:1]

[unnamed.gh|attachment]
rarfile
is not a core Python library. So if you’ve not manually side loaded it, insert a new first line to have the Python 3 component install it from PyPi:
#r: rarfile
As to the rest, check you’re supplying the correct path to the rarfile, and have valid Windows permissions to read from there (and to write to wherever it is you want to write the output file to). I’ve not read this code it or tested it but from a quick glance it appears to be sensible overall. For others’ convenience:
Component 1
import re
import os
import rarfile
def extract_rar_file_path(message):
# Extract the path of the .rar file from the message string
match = re.search(r"saved to: (.*?\.rar)", message)
if match:
return match.group(1)
else:
return None
def search_epw_file_in_rar(rar_path, city_name):
# Open the .rar file
with rarfile.RarFile(rar_path) as rf:
# List all files in the .rar archive
file_list = rf.namelist()
# Search for the .epw file in the "epw" folder that contains the city name
for file in file_list:
if file.startswith("epw/") and city_name in file and file.endswith(".epw"):
return file
return None
def save_epw_file(rar_path, epw_file, destination_folder):
with rarfile.RarFile(rar_path) as rf:
# Extract the .epw file content
epw_content = rf.read(epw_file)
# Define the full destination path
destination_path = os.path.join(destination_folder, os.path.basename(epw_file))
# Write the content to the destination path
with open(destination_path, 'wb') as f:
f.write(epw_content)
return destination_path
# Inputs
message
city_name
# Extract the .rar file path from the message
rar_path = extract_rar_file_path(message)
result = None
if rar_path and os.path.exists(rar_path):
# Search for the .epw file in the .rar archive
epw_file = search_epw_file_in_rar(rar_path, city_name)
if epw_file:
print(f"Found .epw file: {epw_file}")
destination_folder = 'D:\\' # Destination folder on the D: drive
saved_path = save_epw_file(rar_path, epw_file, destination_folder)
print(f".epw file saved to: {saved_path}")
result = saved_path
else:
print("No .epw file found for the given city name.")
result = "No .epw file found for the given city name."
else:
print("Invalid .rar file path or file does not exist.")
result = "Invalid .rar file path or file does not exist."
Component 2
import re
import os
import rarfile
def extract_rar_file_path(message):
# Extract the path of the .rar file from the message string
match = re.search(r"saved to: (.*?\.rar)", message)
if match:
return match.group(1)
else:
return None
def search_epw_file_in_rar(rar_path, city_name):
# Open the .rar file
with rarfile.RarFile(rar_path) as rf:
# List all files in the .rar archive
file_list = rf.namelist()
# Search for the .epw file
for file in file_list:
if file.startswith("epw/") and city_name in file and file.endswith(".epw"):
return file
return None
# Inputs
message
city_name
# Extract the .rar file path from the message
rar_path = extract_rar_file_path(message)
if rar_path and os.path.exists(rar_path):
# Search for the .epw file
epw_file = search_epw_file_in_rar(rar_path, city_name)
if epw_file:
# Construct the save path on Drive D:
save_path = os.path.join("D:", epw_file)
# Extract the .epw file to Drive D:
with rarfile.RarFile(rar_path) as rf:
rf.extract(epw_file, save_path)
print(f".epw file extracted to: {save_path}")
else:
print("No .epw file found for the given city name.")
else:
print("Invalid .rar file path or file does not exist.")
1 Like
Thank you for your prompt reply.
I’ve taken the necessary steps to install the ‘rarfile’ library and load it via the Python script component.
However, I’m encountering the same error. I attempted to replace your code with my own, but unfortunately, I’m still encountering the same issue, which I suspect may be related to permissions.
P.S: I have full administrative access to my Windows system, and I’m running Rhino as an administrator as well.
Check how rarfile expects you to read files fromthe listings in .namelist().
Then construct the simplest possible example, and make sure everything is as you expect at each stage.
2 Likes