Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20110

Re: Reading files in from the proper directory

From Peter Otten <__peter__@web.de>
Subject Re: Reading files in from the proper directory
Date 2012-02-09 22:07 +0100
Organization None
References <f3f576e8-e608-4348-b6ee-fe775d9d1021@x19g2000yqh.googlegroups.com> <mailman.5510.1328641121.27778.python-list@python.org> <9bfb3e39-2bc6-4399-90cc-1c53aa06265e@h6g2000yqk.googlegroups.com> <mailman.5511.1328645799.27778.python-list@python.org> <10d15214-1fd8-4b80-8384-c0b291c6976d@h3g2000yqe.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.5612.1328821689.27778.python-list@python.org> (permalink)

Show all headers | View raw


SMac2347@comcast.net wrote:

> On Feb 7, 3:16 pm, Peter Otten <__pete...@web.de> wrote:
>> SMac2...@comcast.net 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
>> necessary.
>>
>> > 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

Sorry, I was wrong here. "\09" is actually "\0" (i. e. chr(0))
followed by "9". Escape sequences starting with 0 are octal numbers
in Python 2 and thus may never contain digits > 7.

>> 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.
> 
> Peter, thanks so much for your help, your suggestions were spot on. So
> now my program runs and is able to find and process the files
> correctly, but I end up getting the following message:
> 
> Traceback (most recent call last):
>   File "C:/Documents and Settings/smacdon/My Documents/
> excel_merge_files_indirectory v2.py", line 49, in <module>
>     merge_xls(in_dir=r"C:\Documents and Settings\smacdon\Desktop\09
> Aggregate JWS")
>   File "C:/Documents and Settings/smacdon/My Documents/
> excel_merge_files_indirectory v2.py", line 36, in merge_xls
>     merged_book.save(out_file)
>   File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 634, in
> save
>     doc.save(filename, self.get_biff_data())
>   File "C:\Python27\lib\site-packages\xlwt\CompoundDoc.py", line 507,
> in save
>     f = open(file_name_or_filelike_obj, 'wb')
> TypeError: file() argument 1 must be encoded string without NULL
> bytes, not str
> 
> 
> If I am interpreting correctly, am I to understand that it would
> appear the issue is tracing back to functions in the xlwt module? If
> so, what can I do to fix this? Again, any and all help is appreciated!

You probably forgot to convert the default value for out_file into a 
raw string:

def merge_xls(in_dir, out_file=
r"C:\Documents and Settings\smacdon\Desktop\09 Aggregate JWS\09_merged_data.xls"):

"\0" is therefore interpreted as chr(0) which marks the end of a C string and 
may not occur in a file name. chr(0) is called "NULL byte" in the error message 
you get.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Reading files in from the proper directory SMac2347@comcast.net - 2012-02-07 10:14 -0800
  Re: Reading files in from the proper directory Dave Angel <d@davea.name> - 2012-02-07 13:40 -0500
    Re: Reading files in from the proper directory SMac2347@comcast.net - 2012-02-07 11:03 -0800
  Re: Reading files in from the proper directory Peter Otten <__peter__@web.de> - 2012-02-07 19:59 +0100
    Re: Reading files in from the proper directory SMac2347@comcast.net - 2012-02-07 11:13 -0800
      Re: Reading files in from the proper directory John Gordon <gordon@panix.com> - 2012-02-07 20:00 +0000
      Re: Reading files in from the proper directory Peter Otten <__peter__@web.de> - 2012-02-07 21:16 +0100
        Re: Reading files in from the proper directory SMac2347@comcast.net - 2012-02-09 12:32 -0800
          Re: Reading files in from the proper directory Peter Otten <__peter__@web.de> - 2012-02-09 22:07 +0100
          Re: Reading files in from the proper directory Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-02-09 23:05 -0500
        Re: Reading files in from the proper directory SMac2347@comcast.net - 2012-02-09 12:36 -0800
  Re: Reading files in from the proper directory John Gordon <gordon@panix.com> - 2012-02-07 19:46 +0000

csiph-web