Reading files in from the proper directory

Peter Otten __peter__ at
Tue Feb 7 21:16:57 CET 2012

SMac2347 at wrote:

> xls_files   = glob.glob(in_dir + "*.xls")

Try changing that to

pattern = os.path.join(in_dir, "*.xls")
xls_files = glob.glob(pattern) 

os.path.join() inserts a (back)slash between directory and filename if 

> merge_xls(in_dir="C:\Documents and Settings\smacdon\Desktop\09 Aggregate JWS")

If you paste the directory name literal into the interactive interpreter 
you'll be surprised:

>>> "C:\Documents and Settings\smacdon\Desktop\09 Aggregate JWS"
'C:\\Documents and Settings\\smacdon\\Desktop\x009 Aggregate JWS'

"\09" is intrpreted as chr(9). Use a raw string to prevent Python from 
interpreting a backslash as the start of an escape sequence

>>> r"C:\Documents and Settings\smacdon\Desktop\09 Aggregate JWS"
'C:\\Documents and Settings\\smacdon\\Desktop\\09 Aggregate JWS'

or use forward slashes as directory separators.

More information about the Python-list mailing list