Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'escape': 0.04; 'encoded': 0.05; 'interpreter': 0.05; 'inserts': 0.07; 'python': 0.08; 'backslash': 0.09; 'bytes,': 0.09; 'filename': 0.09; 'interpreting': 0.09; 'module?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:files': 0.09; 'tracing': 0.09; 'def': 0.13; 'argument': 0.15; 'skip:" 40': 0.15; '36,': 0.16; '49,': 0.16; 'appreciated!': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'slashes': 0.16; 'string:': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'this?': 0.19; '(most': 0.21; 'thus': 0.21; 'feb': 0.22; 'runs': 0.23; 'appear': 0.23; 'from:addr:web.de': 0.23; 'interpreted': 0.23; 'literal': 0.23; 'skip:m 30': 0.24; 'string': 0.24; 'traceback': 0.24; 'fix': 0.25; 'convert': 0.25; 'aggregate': 0.29; 'forgot': 0.29; 'sorry,': 0.29; "skip:' 10": 0.29; 'pm,': 0.29; 'get.': 0.30; 'pattern': 0.30; 'str': 0.30; 'typeerror:': 0.30; 'error': 0.30; 'actually': 0.31; 'thanks': 0.32; 'changing': 0.32; 'file': 0.34; 'header:X -Complaints-To:1': 0.34; 'last):': 0.34; 'null': 0.34; 'probably': 0.35; 'to:addr:python-list': 0.35; 'help,': 0.35; 'starting': 0.36; 'received:org': 0.36; 'issue': 0.37; 'sequence': 0.37; 'but': 0.37; 'skip:" 10': 0.37; 'necessary.': 0.38; 'getting': 0.38; 'skip:o 20': 0.38; 'files': 0.39; 'subject:from': 0.39; 'suggestions': 0.39; 'called': 0.40; 'raw': 0.40; 'to:addr:python.org': 0.40; 'back': 0.60; "you'll": 0.61; 'your': 0.61; 'skip:o 30': 0.63; 'forward': 0.63; 'here.': 0.64; 'spot': 0.79; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Reading files in from the proper directory Date: Thu, 09 Feb 2012 22:07:58 +0100 Organization: None References: <9bfb3e39-2bc6-4399-90cc-1c53aa06265e@h6g2000yqk.googlegroups.com> <10d15214-1fd8-4b80-8384-c0b291c6976d@h3g2000yqe.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084adf6.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328821689 news.xs4all.nl 6880 [2001:888:2000:d::a6]:48591 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20110 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 > 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.