473,425 Members | 1,560 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,425 software developers and data experts.

lists and files question

This code:

import os, re, string
setpath = raw_input("Enter the path: ")
for root, dirs, files in os.walk(setpath):
id = re.compile('Microsoft Excel Worksheet')
fname = files
# print fname
content = open(fname[1],'rb')

Produces this error:

IOError: Error[2] No such file or directory 'Name of File'

The strange thing is that it correctly identifies the file that it says
doesn't exist. Could someone explain why this is?

Also, is "files" a nested list? It looks like one, but I'm not entirely
sure as I'm still relatively new to Python. Thanks!

Jul 18 '05 #1
4 1946
In article <3F**************@hotmail.com>, hokiegal99
<ho********@hotmail.com> wrote:
This code:

import os, re, string
setpath = raw_input("Enter the path: ")
for root, dirs, files in os.walk(setpath):
id = re.compile('Microsoft Excel Worksheet')
fname = files
# print fname
content = open(fname[1],'rb')

Produces this error:

IOError: Error[2] No such file or directory 'Name of File'

The strange thing is that it correctly identifies the file that it says
doesn't exist. Could someone explain why this is?
The problem is that file doesn't exist in the current working
directory; it's in another directory (stored in "root" in your code).

Try this:
content = open(os.path.join(root,fname[1]), 'rb')
Also, is "files" a nested list? It looks like one, but I'm not entirely
sure as I'm still relatively new to Python. Thanks!


It is a list of strings. Each string is the name of one of the files
in the directory (whose path is in "root" above).

-Mark
Jul 18 '05 #2
On Tuesday 22 July 2003 16:57, hokiegal99 wrote:
This code:

import os, re, string
setpath = raw_input("Enter the path: ")
for root, dirs, files in os.walk(setpath):
id = re.compile('Microsoft Excel Worksheet')
fname = files
# print fname
content = open(fname[1],'rb')

Produces this error:

IOError: Error[2] No such file or directory 'Name of File'


if you replace your logic with some prints you will quickly see the problem.

What happens is os.walk() passes filenames without their path. You need to
use os.path.join() to get the full name back.
Jul 18 '05 #3

"print fname" prints out the list of files in "setpath" w/o problem. How
does it do that if os.walk doesn't give it the path to the files?

Here's some output from "print fname":

['index.txt', 'CELL-MINUTES.xls', '.nautilus-metafile.xml']
['2000 Schedule.xls', '2001 State.pdf', '2001.pdf', 'A Little More Like
Bill.doc', 'AARP.doc', "Accounting's of Dad's Est.xls",
'Amortization_Table.xls', 'huey letter.doc', 'utt-R&D.pdf', 'utt.pdf',
'rest.xls', 'Debts and Assets.xls', 'First Accounting - Estate.xls',
'Friends.doc', "Grace's.doc", 'Home Address.doc', 'Ins.doc',
'Insurance.doc', 'Interro.doc', 'Marshall.doc', 'NBB home loan.doc',
'Position Description.doc', "andy's", "Andy's Travel Voucher.xls",
"Andy's Travel.xls", 'Rebuttal.doc', 'Refinance.doc', 'TaxReturn 2.pdf',
'TaxReturn 3.pdf', 'TaxReturn 4.pdf', 'TaxReturn 5.pdf',
'TaxReturn.pdf', 'The Casey Song.doc', "Touch of the Hand.xls", 'Workout
Sheet.xls', 'application.pdf', 'budsum.pdf']

When I add os.path.join like this:

setpath = raw_input("Enter the path: ")
for root, dirs, files in os.walk(setpath):
id = re.compile('Microsoft Excel Worksheet')
fname = files
print fname
content = open(os.path.join(root,fname[0]),'rb')

I get a "IndexError: list index out of range" error.

This is a Linux 2.4 computer running Python 2.3b2... if that matters.

Thanks!

Sean 'Shaleh' Perry wrote:
On Tuesday 22 July 2003 16:57, hokiegal99 wrote:
This code:

import os, re, string
setpath = raw_input("Enter the path: ")
for root, dirs, files in os.walk(setpath):
id = re.compile('Microsoft Excel Worksheet')
fname = files
# print fname
content = open(fname[1],'rb')

Produces this error:

IOError: Error[2] No such file or directory 'Name of File'

if you replace your logic with some prints you will quickly see the problem.

What happens is os.walk() passes filenames without their path. You need to
use os.path.join() to get the full name back.



Jul 18 '05 #4
Thanks to everyone for the feedback on this. I've learned a lot from you
guys.

John Hunter wrote:
Others have already answered your question - I just want to point out
a few of other things

import os, re, string
setpath = raw_input("Enter the path: ")
for root, dirs, files in os.walk(setpath):
id = re.compile('Microsoft Excel Worksheet')

1) id is a built in function; you may not want to override it with
your variable name
>>> x = 1
>>> id(x) 135313208

2) The reason to use re.compile is for efficiency. There is no need
to call it inside the loop, since you're just recompiling the same
regex over and over again. Instead, compile the regex outside the
loop
>>> rgx = re.compile('[A-Z]+')
>>> for some_text in some_list: ... m = rgx.match(some_text)

3) If you want to match 'Microsoft Excel Worksheet', you don't need
regular expressions since this is a string literal. You will
probably be better off just using the string find method, as in

s.find('Microsoft Excel Worksheet')

4) You may want to look at the path module, which provides a nice
interface for walking over files:
http://www.jorendorff.com/articles/python/path/
>>> from path import path
>>> xldir = path(setpath)
>>> for f in xldir.files('*.xls'):

... print f.read().find('Microsoft Excel Worksheet')

Cheers,
John Hunter


Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
19
RMWChaos
by: RMWChaos | last post by:
Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to...
14
by: etal | last post by:
Here's an algorithm question: How should I efficiently merge a collection of mostly similar lists, with different lengths and arbitrary contents, while eliminating duplicates and preserving order...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.