【问题标题】:create a file outside the directory in python [closed]在python目录之外创建一个文件[关闭]
【发布时间】:2014-01-16 11:12:25
【问题描述】:

我想在 python 的当前工作目录之外创建一个文件。这是我的目录结构。

|--myproject
|   |-- gui
|   |   |-- modules
|   |   |   |-- energy
|   |   |   |   |-- configuration
|   |   |   |   |   |-- working_file.py
|   |-- service
|   |   |-- constants
|   |   |   |-- global_variables.json    

我目前在/myproject/gui/energy/configuration/working_file.py 工作,我想在/myproject/service/constants 下创建一个名为global_variables.json 的文件

我试过了

with open("../../../../../service/constants/global_variables.json", 'w') as file_handler:
        content = json.load(file_handler)

【问题讨论】:

  • 你在哪里写入文件句柄?
  • 什么?我没找到你。
  • 1) 检查当前工作目录 2) 你想在哪里创建它 3) 检查它的实际创建位置 4) 利润(修复路径)
  • @KarolyHorvath- 好吧,问题是根本没有创建文件,这就是我发布的原因。因此,我自己会跟随你的步骤,
  • 您说您要创建一个文件,但实际上您正在读取一个文件,该文件是您在w(写入模式)中打开的。那你想做什么?

标签: python file


【解决方案1】:

对于下面给出的项目结构:

.
`-- myproject
    |-- gui
    |   `-- energy
    |       `-- configuration
    |           `-- test.py
    `-- services
        `-- constants
            `-- out.txt


import os

## Finding absolute path of the current module
drive, tcase_dir = os.path.splitdrive(os.path.abspath(__file__))

## It's good if we always traverse from the project root directory
## rather than the relative path
## So finding the Project's root directory
paths = tcase_dir.split(os.sep)[:-4]
base_dir = os.path.join(drive,os.sep,*paths)


## Known Sub-Directories
SERVICES_DIR = r'services'
CONSTANTS_DIR = r'constants'

## absolute path to the ../myproject/service/constants/ directory
constants_abs_path = os.path.join(base_dir, SERVICES_DIR, CONSTANTS_DIR)

with open(os.path.join(constants_abs_path, r'out.txt'), 'r') as fp:
    ## Do the file Operations here ##

【讨论】:

    【解决方案2】:

    你可以做的是从该路径中找到当前文件路径和脚本目录路径,像这样

    dir = os.path.dirname(__file__)
    

    然后你可以添加或加入你想要创建文件的地方,相应的路径

    jsonfilepath = "../../../../../service/constants/global_variables.json"
    
    reljsonfilepath = os.path.join(dir, jsonfilepath)
    
    f = open (reljsonfilepath, 'w')
    

    请检查这是未经测试的代码。

    【讨论】:

      【解决方案3】:

      你确定路径正确吗?

      假设当前路径是: ../myproject/gui/modules/energy/configuration

      你提到的路径是:

      "..(a)/..(b)/..(c)/..(d)/..(e)/service/constants/global_variables.json"  
      
      (a) = energy/
      (b) = modules/
      (c) = gui/
      (d) = myproject/
      (e) = ../
      

      我认为您的服务目录在 myproject 目录中,而不是在它之前的目录中。不确定这是否是您的问题……这是您的问题吗?

      【讨论】:

        【解决方案4】:

        Python 不会解释 ../,它会在 cwd 中查找名为“..”的目录。

        你要么必须硬编码路径:

        with open("/path/to/myproject/service/constants/global_variables.json", 'w') as file_handler:
            content = json.load(file_handler)
        

        或者找到当前执行脚本的完整路径:

        编辑:我错了,python 确实解释了“..”,这里发生的是 start 是 cwd 而不是你的脚本。

        $ echo 'Hello world' > text_file.txt
        $ mkdir test/
        $ cd test
        $ python
        [...]
        >>> open('../text_file.txt').read()
        'Hello world\n'
        

        【讨论】:

          【解决方案5】:

          相对路径是从当前工作目录解析的,而不是从脚本所在的目录解析的。如果您尝试创建的文件需要位于特定目录中,请使用 绝对 路径(例如 /absolute/path/to/myproject/service/constants/global_variables.json)。

          如果您无法知道这个绝对路径,请参考this SO question

          【讨论】:

            猜你喜欢
            • 2013-01-12
            • 1970-01-01
            • 1970-01-01
            • 2015-07-25
            • 2020-07-28
            • 1970-01-01
            • 2021-08-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多