Methods to avoid hard-coding file paths in Python

时间:2017-08-04 13:13:15

标签: python

Working with scientific data, specifically climate data, I am constantly hard-coding paths to data directories in my Python code. Even if I were to write the most extensible code in the world, the hard-coded file paths prevent it from ever being truly portable. I also feel like having information about the file system of your machine coded in your programs could be security issue.

What solutions are out there for handling the configuration of paths in Python to avoid having to code them out explicitly?

4 个答案:

答案 0 :(得分:2)

其中一个解决方案依赖于使用配置文件。

您可以将所有路径存储在json文件中,如下所示:

{
     "base_path" : "/home/bob/base_folder",
     "low_temp_area_path" : "/home/bob/base/folder/low_temp"
}

然后在你的python代码中,你可以这样做:

import json

with open("conf.json") as json_conf : 
    CONF = json.load(json_conf)

然后您可以使用您的路径(或您喜欢的任何配置变量),如下所示:

print "The base path is {}".format(CONF["base_path"])

答案 1 :(得分:1)

I believe there are many ways around this, but here is what I would do:

  1. Create a JSON config file with all the paths I need defined.
  2. For even more portability, I'd have a default path where I look for this config file but also have a command line input to change it.

答案 2 :(得分:1)

在我看来,从命令行传递参数将是最好的解决方案。你应该看一下argparse。这允许您创建从命令行处理参数的好方法。例如: myDataScript.py /home/userName/datasource1/

答案 3 :(得分:0)

首先,它始终是一个很好的做法,即在每个类中添加一个主函数来测试文件中的该类或函数。与此一同确定当前的工作目录。从cron作业或不是当前工作目录的目录运行python时,这变得异常重要。这样就不需要JSON文件或环境变量,您将可以在Mac,RHEL和Debian发行版之间实现互操作。

这是您的操作方式,如果您使用'\'而不是'/',它也将在Windows上运行(如果您的情况下甚至是必要的话)。

/**
 * @ApiResource(
 *     itemOperations={
 *          "get"
 *     },
 *     collectionOperations={
 *          "post"={
 *              "method"="POST",
 *              "path"="/calcul",
 *              "controller"=CalculController::class
 *          }
 *     },
 *     normalizationContext={"groups"={"output"}},
 *     denormalizationContext={"groups"={"input"}}
 * )
 */
class Calcul
//..

运行命令时可以看到,如果提供完整路径或相对路径,则会计算工作目录,这意味着它将自动在cron作业中工作。

之后,如果要使用存储在当前目录中的数据,请使用:

if "__main__" == __name__:
    workingDirectory  = os.path.realpath(sys.argv[0])

,或者在上述工作目录(与您的项目目录平行)中:

fileName = os.path.join( workingDirectory, './sub-folder-of-current-directory/filename.csv' )
fp       = open( fileName,'r')