Python XLRD no such file or directory - python

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

Related

File Opening - Script still finds file in cwd even when absolute path specified

I have looked at How to open a list of files in Python This problem similar but not covered.
path = "C:\\test\\test5\\"
files = os.listdir(path)
fileNames = []
for f in files:
fileNames.append(f)
for fileName in fileNames:
pathFileName = path + fileName
print(f"This is the path: {pathFileName}")
fin = open(pathFileName, 'rt')
texts = []
with open(fileName) as file_in:
# read file text lines into an array
for text in file_in:
texts.append(text)
for text in texts:
print(text)
The file aaaatest.txt is in C:\test\test5 The output is:
This is the path: C:\test\test5\aaaatest.txt
Traceback (most recent call last):
File "c:\Users\david\source\repos\python-street-spell\diffLibFieldFix.py", line 30, in <module>
with open(fileName) as file_in:
FileNotFoundError: [Errno 2] No such file or directory: 'aaaatest.txt'
So here's the point. If I take a copy of aaaatest.txt (leaving original where it is) and put it in the current working directory. Running the script again I get:
This is the path: C:\test\test5\aaaatest.txt
A triple AAA test
This is the path: C:\test\test5\AALTONEN-ALLAN_PENCARROW_PAGE_1.txt
Traceback (most recent call last):
File "c:\Users\david\source\repos\python-street-spell\diffLibFieldFix.py", line 30, in <module>
with open(fileName) as file_in:
FileNotFoundError: [Errno 2] No such file or directory: 'AALTONEN-ALLAN_PENCARROW_PAGE_1.txt'
The file aaaatest.txt is opened and the single line of text, contained in it, is outputted. Following this an attempt is made to open the next file of C:\test\test5 where the same error occurs again.
Seems to me that while the path is saying C:\test\test5 the file is only being read from the cwd?

FileNotFoundError in opening txt file with python

I am trying to open a txt file for reading with this code:-
type_comments = [] #Declare an empty list
with open ('society6comments.txt', 'rt') as in_file: #Open file for reading of text data.
for line in in_file: #For each line of text store in a string variable named "line", and
type_comments.append(line.rstrip('\n')) #add that line to our list of lines.
Error:-
Error - Traceback (most recent call last):
File "c:/Users/sultan/python/society6/society6_promotion.py", line 6, in <module>
with open ('society6comments.txt', 'rt') as in_file:
FileNotFoundError: [Errno 2] No such file or directory: 'society6comments.txt'
I already have a file name with 'society6comments.txt' in the same directory has my script so why is it showing error?
The fact that the text file is in the same directory as your program does not make that directory the current working directory. Put the full path to the file in your open() call.
You can use os.path.dirname(__file__) to obtain the directory name of the script, and then join the file name you want:
import os
with open (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'society6comments.txt'), 'rt') as in_file:

Extract a .TAR file within a .gz file

I have to untar around fifty *.gz files in a directory. Inside each *.gz file there is a *.TAR file and some other files.
I am trying a python script which extracts the contents of the *.gz files to a directory. But, I am not able to extract the *.TAR files inside the same directory to which the contents of *.gz are extracted.
This is how the script looks:
import tarfile
import os
import glob
basedir = "path_to _dir"
for i in glob.glob(basedir +"*.gz"):
a = os.path.basename(i)
b = os.path.splitext(a)[0]
c = os.path.splitext(b)[0]
os.mkdir(os.path.join(basedir,c))
t1 = tarfile.open(i)
t1.extractall(c)
for j in os.listdir(c):
if j.endswith('.TAR'):
print(j)
t2 = tarfile.open(j)
t2.extractall()
t2.close()
t1.close()
Its giving me the error:
Traceback (most recent call last):
File "./untar.py", line 16, in <module>
t2 = tarfile.open(j)
File "/usr/lib64/python2.7/tarfile.py", line 1660, in open
return func(name, "r", fileobj, **kwargs)
File "/usr/lib64/python2.7/tarfile.py", line 1722, in gzopen
fileobj = bltn_open(name, mode + "b")
IOError: [Errno 2] No such file or directory: '0299_0108060501.TAR'
0299_0108060501.TAR is the file contained inside the *.gz file
It seems to me that I am doing something very wrong fundamentally, but I don't know what.
Since tar.gz files are TAR archives compressed with gzip one should use
t1 = tarfile.open(i, 'r:gz')
as per documentation.
Also, you need to combine the path of the inner file with the directory being inspected, like so:
t2 = tarfile.open(os.path.join(c, j))

getting error while unraring with patoolib on 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.

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))

Categories

Resources