lists and files question
hokiegal99
hokiegal99 at hotmail.com
Tue Jul 22 22:29:44 EDT 2003
"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.
More information about the Python-list
mailing list