0

I was wondering what the best practices are on working with paths in the following scenario: I can either choose to change the current directory to the desired folder and then generate a file using only the file name, or just use the full path directly.

Here is the code where I set the current directory os.chdir():

a=time.clock()
import os
for year in range(start,end):
 os.chdir("C:/CO2/%s" % year)
 with open("Table.csv",'r') as file:
 content=file.read()
b=time.clock()
b-a
Out[55]: 0.002037443263361638

And that is slower than when using the full path directly:

a=time.clock()
for year in range(start,end):
 with open("C:/CO2/%s/Table.csv" % year,'r') as file:
 content=file.read()
b=time.clock()
b-a
Out[56]: 0.0014569102613677387

I still doubt though whether using the full path is good practice. Are both the methods cross platform? Should I be using os.path instead of %s?

asked Feb 10, 2015 at 11:04
4
  • I doubt that putting the absolute part C:/CO2/ in your code is a good idea. The rest doesn't matter. Commented Feb 10, 2015 at 11:05
  • 1
    I would use os.path.join Commented Feb 10, 2015 at 11:06
  • @LutzHorn, sometimes I get a path with inconsistent slash separators such as C:/CD2\2001円 Would it probably be better to just chdir to C:/CD2 and then work on the file? Commented Feb 11, 2015 at 9:31
  • @PadraicCunningham you mean this os.chdir(os.path.join("C:","CD2")) ? Commented Feb 11, 2015 at 9:58

2 Answers 2

2

What's the use case for the code in question? Is it a script invoked on the command line by a user? If so, I would usually take the path as a command-line argument (sys.argv), as a command-line option (argparse), or using some sort of configuration file.

Or is the file path part of a more general-purpose module? In that case, I might think about wrapping the path and related code in a class (class FooBar). Users of the module could pass in the needed file path information when instantiating a FooBar. If users tended to use the same path over and over, I would again lean toward a strategy based on a configuration file.

Either way, the file path would be separate from the code -- at least for real software projects.

If we're talking about a one-off script with very few users and almost zero likelihood of future evolution or code re-use, it does not matter too much what you do.

answered Feb 10, 2015 at 11:35
Sign up to request clarification or add additional context in comments.

Comments

1

As @lutz-horn said, hardcoded path isn't good idea for any code, except single-run scripts.

Talking about design, choose the methods that seem to be more explicit and simple for further development, don't optimize your code until run time becomes an issue.

In particular case, I'd prefer second way. No need to chdir until you're writing consistent files. You should use explicit chdir in case you're writing many files with different name schemas.

answered Feb 10, 2015 at 11:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.