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


Groups > comp.lang.python > #19974

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-07 19:59 +0100
Organization None
References <f3f576e8-e608-4348-b6ee-fe775d9d1021@x19g2000yqh.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.5510.1328641121.27778.python-list@python.org> (permalink)

Show all headers | View raw


SMac2347@comcast.net wrote:

> Hello. I am admittedly a Python novice, and ran into some trouble
> trying to write a program that will pull multiple excel files all into
> one file, with each file on a different sheet.
> 
> I am confident most of the code is correct, as the program runs
> without any errors and I found the base of it online, making changes
> as necessary for my own purposes. 

That confidence usually evaporates once you write the first unit test ;)

> However, I am having trouble
> specifying the exact directory where my code should be pulling the
> files from. All the files are in the same folder, and I have put the
> folder on my desktop. Am I correct in thinking that I need to change
> the current working directory to this folder in order for Python to
> read in these files, then generate my output? Or should I be doing
> something else?

Do it properly, allow specifying the files on the commandline:

import argparse

def process_files(files, destfile):
    # put your real code here
    print "merge " + "\n      ".join(files)
    print "into  " + destfile

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("files", metavar="file", nargs="+")
    parser.add_argument("destfile")
    args = parser.parse_args()
    process_files(args.files, args.destfile)

If you have standard locations for sources and destination you can wrap your 
python script into a little batch file containing something like

python \source\path\*.xls \dest\path\merged.xls

and invoke that to get both flexibility and convenience.

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