getting error while unraring with patoolib on Python - python

This code is just for testing purposes ( learning here )
Checks for files on a directory, if they end with .rar it prints its name, decompresses the file and calls another function that prints the name again.
The code :
import os, patoolib
path = "/root/tree/down/"
def cutext(f):
print (f)
def unrar():
for f in os.listdir(path):
if f.endswith('.rar'):
print(f)
patoolib.extract_archive(f, outdir="/root/tree/def")
cutext(f)
unrar()
The output error ( reads the file but does not decompresses it ) :
root#debian:~# python autotube.py
Ravi Shankar - Shankar Family & Friends (2010) [FLAC].rar
Traceback (most recent call last):
File "autotube.py", line 17, in <module>
unrar()
File "autotube.py", line 14, in unrar
patoolib.extract_archive(f, outdir="/root/tree/def")
File "/usr/local/lib/python3.4/dist-packages/patoolib/__init__.py", line 676, in extract_archive
util.check_existing_filename(archive)
File "/usr/local/lib/python3.4/dist-packages/patoolib/util.py", line 390, in check_existing_filename
raise PatoolError("file `%s' was not found" % filename)
patoolib.util.PatoolError: file `Ravi Shankar - Shankar Family & Friends (2010) [FLAC].rar' was not found

patoolib.util.PatoolError: file `Ravi Shankar - Shankar Family & Friends (2010) [FLAC].rar' was not found
This suggests the program can not find the file. You are doing listdir() on "/root/tree/down/" but when you're passing the file to extract_archive, you're just passing the file name f which does not include the full path. So if there's a file /root/tree/down/myarchive.rar, you're just passing myarchive.rar.
So the program tries to find it in the same directory (current working directory) but can not find it since it's not here.
Please update your code to pass the full path:
def unrar():
for f in os.listdir(path):
if f.endswith('.rar'):
print(f)
patoolib.extract_archive(os.path.join(path,f), outdir="/root/tree/def")
cutext(f)
The os.path.join should construct the full path to the file and pass it. Now your program should work.

Related

Owlrady2 Load imported onotologies from file not from url

I'm trying to load an ontology that imports some other ontologies. These other ontologies are located on my machine and not on the web. If I try my_ontology.load() it tries to load these other ontologies from their URI. But that fails.
How can I tell owlready2 where these ontologies are located?
The traceback looks like this:
Traceback (most recent call last):
File "~Software/PyOntologyChecker/app.py", line 12, in <module>
covid_onto = get_ontology_from_file(COVID_ONTOLOGY)
File "~Software/PyOntologyChecker/app.py", line 8, in get_ontology_from_file
return get_ontology("file://" + file_path).load()
File "~.local/lib/python3.8/site-packages/owlready2/namespace.py", line 813, in load
imported_ontologies = [self.world.get_ontology(self._unabbreviate(abbrev_iri)).load() for abbrev_iri in self.world._get_obj_triples_sp_o(self.storid, owl_imports)]
File "~.local/lib/python3.8/site-packages/owlready2/namespace.py", line 813, in <listcomp>
imported_ontologies = [self.world.get_ontology(self._unabbreviate(abbrev_iri)).load() for abbrev_iri in self.world._get_obj_triples_sp_o(self.storid, owl_imports)]
File "~.local/lib/python3.8/site-packages/owlready2/namespace.py", line 773, in load
except: raise OwlReadyOntologyParsingError("Cannot download '%s'!" % f)
owlready2.base.OwlReadyOntologyParsingError: Cannot download 'http://ontofox.hegroup.org/CHEBI_module11.owl'!
Assuming you have a local copy of CHEBI_module11.owl dependence (ex. SOME_DIR/HEBI_module11.owl), add its location into onto_path (as stated in the docs):
onto_path.append("SOME_DIR")
before doing my_ontology.load(). That tells Owlready2 to look for files in SOME_DIR. The path can be relative or absolute (like c:/data/ontologies).
The current Python's working directory is not scanned by default but can be set so with onto_path.append(".").
If a required ontology is described by URI not ending with a usual filename, like "http://rdf.webofcode.org/woc/", the local filename (woc in this case) should not have an extension.

Error message when using openpyxl.load_workbook() function

I have just started to learn programming and I am currently trying to read an excel file from IDLE. I'm following instruction from the book "Automate the Boring Stuff". I have successfully imported openpyxl, and thereafter, as instructed tried wb = openpyxl.load_workbook('example.xlsx') where I exchanged "example" to the actual name of the workbook. However, I get this error message:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 117, in load_workbook
archive = ZipFile(filename, 'r', ZIP_DEFLATED) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1216, in __init__
self.fp = io.open(file, filemode) FileNotFoundError: [Errno 2] No such file or directory: 'jan.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
wb = openpyxl.load_workbook('jan.xlsx')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 145, in load_workbook
raise InvalidFileException(unicode(e))
openpyxl.exceptions.InvalidFileException: [Errno 2] No such file or directory: 'jan.xlsx'
I don't understand how to solve this.
This error message simply says that python can't locate the file. When you try to open a file 'jan.xlsx', it's trying to locate it in the base directory of your code in your IDE. So say your code is in a directory called /Users/username/PycharmProjects/myCode
(I'm assuming here you are on a Mac OS as the path to your python suggests...
but jan.xlsx is in /Users/username
Since that is 2 directories up from your code directory, you can do one of two things:
Write in the absolute path to the file:
wb = openpyxl.load_workbook('/Users/username/jan.xlsx')
Use a relative path that is relative to the base project directory. Two dots in a relative path means one level up from the current directory. So if the excel file is 2 levels up, you can do:
wb = openpyxl.load_workbook('../../jan.xlsx')

IOError: [Errno 2] Python script to parse txt files

I'm starting a python script to parse a number of small text files in a folder. I need to retrieve particular information that will always be different (in this case hostname, model & serial number for a Cisco switch) and so can't use a regular expression. However I can easily find the line that contains the information. This is what I have so far:
import os
def parse_files(path):
for filename in os.listdir(path):
with open(filename,'r').read() as showfile:
for line in showfile:
if '#sh' in line:
hostname = line.split('#')[0]
if 'Model Number' in line:
model = line.split()[-1]
if 'System serial number' in line:
serial = line.split()[-1]
showfile.close()
path = raw_input("Please specify Show Files directory: ")
parse_files(path)
print hostname,model,serial
This, however, is returning:
Traceback (most recent call last):
File "inventory.py", line 17, in <module>
parse_files(path)
File "inventory.py", line 5, in parse_files
with open(filename,'r').read() as showfile:
IOError: [Errno 2] No such file or directory: 'Switch01-run.txt'
where 'Switch01-run.txt' is a file in the specified folder. I can't figure out where I'm taking a wrong turn.
The problem is that os.listdir() is returning the filenames from the directory, not the complete path to the file.
You need to do this instead:
with open(os.path.join(path,filename),'r') as showfile:
This fixes two issues - the IOerror, and the error you will get trying to read lines from a string.

Parse XML Tag value for all files in directory using Python

I can't quite make the leap despite pre-existing similar questions. Help would be valued!
I am trying to recursively parse all xml files in the directory/sub directory
I am looking for the value that appears for the tag "Operator id"
Example source XML:
<Operators>
<Operator id="OId_LD">
<OperatorCode>LD</OperatorCode>
<OperatorShortName>ARRIVA THE SHIRES LIMIT</OperatorShortName>
This is the code I have thus far:
from xml.dom.minidom import parse
import os
def jarv(target_folder):
for root,dirs,files in os.walk(target_folder):
for targetfile in files:
if targetfile.endswith(".xml"):
print targetfile
dom=parse(targetfile)
name = dom.getElementsByTagName('Operator_id')
print name[0].firstChild.nodeValue
This is the terminal command I am running:
python -c "execfile('xml_tag.py'); jarv('/Users/admin/Projects/AtoB_GTFS')"
And this is the error I receive:
tfl_64-31_-37434-y05.xml
encodings.xml
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "xml_tag.py", line 8, in jarv
dom=parse(targetfile)
File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/minidom.py", line 1918, in parse
return expatbuilder.parse(file)
File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 922, in parse
fp = open(file, 'rb')
IOError: [Errno 2] No such file or directory: 'encodings.xml'
(frigo)andytmac:AtoB_GTFS admin$ python -c "execfile('xml_tag.py'); jarv('/Users/admin/Projects/AtoB_GTFS')"
tfl_64-31_-37434-y05.xml
If I comment out the code after the 'print target file' line it does list all the xml files I have.
Thanks for your assistance,
Andy
You're not looking at the right place (relative path) : when you use for root, dirs, files in os.walk(target_folder):, files is a list of the file names in the directory root, and not their absolute path.
Try remplacing dom=parse(targetfile) by dom = parse(os.sep.join(root, targetfile))

Python XLRD no such file or directory

I have a script working where I can open files if I pass the file name but now the number of files is increasing and it doesn't make sense to have to run the script on each file individually. So I decided to let python read al files in a directory.
for root, dirs, files in os.walk("Approved_LRPMP_Worksheets/"):
for fyle in files:
if fyle.endswith(".xlsx"):
print fyle
book = xlrd.open_workbook(fyle)
print "book opened"
The output from this is:
I found a file: Agoura Hills LRPMP Review Sheet.xlsx
Traceback (most recent call last):
File "test.py", line 21, in <module>
book = xlrd.open_workbook(fyle)
File "/Library/Python/2.7/site-packages/xlrd/__init__.py", line 394, in open_workbook
f = open(filename, "rb")
IOError: [Errno 2] No such file or directory: 'Agoura Hills LRPMP Review Sheet.xlsx'
The file is clearly being read, the name of the file is output in the command line just before I get the error. I feel like this is something simple that I should be catching but it has evaded me for the last 30 minutes...
It was a simple fix, I was being stupid.
for root, dirs, files in os.walk("Approved_LRPMP_Worksheets/"):
for fyle in files:
if fyle.endswith(".xlsx"):
print "I found a file: " + fyle
fyleName = "Approved_LRPMP_Worksheets/"+fyle
book = xlrd.open_workbook(fyleName)
And its done

Categories

Resources