Reading files in from the proper directory
d at davea.name
Tue Feb 7 19:40:10 CET 2012
On 02/07/2012 01:14 PM, SMac2347 at 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. 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,
No, Python certainly does not constrain you to working with files only
in the current working directory. My rule of thumb is never to change
the cwd in a Python program. You can use relative paths to open files,
or you can use absolute paths. There is even a library function
os.path.abspath() for converting a relative path to an absolute one.
If you do change cwd during the running of a program, then relative
filenames that worked earlier might no longer work. You could convert
them all to absolute paths, but that's more work.
You can piece together path strings using os.path.join(). It's smart
enough to know the path separator for your particular platform.
Check out this page: http://docs.python.org/library/os.path.html
> then generate my output? Or should I be doing
> something else?
> Any and all help is appreciated, thanks!
More information about the Python-list