python创建一个目录_在python目录外创建一个文件

在Python中,从当前工作目录/myproject/gui/energy/configuration/working_file.py出发,要在/myproject/service/constants下创建global_variables.json文件,需要使用正确的路径。避免使用相对路径,而应使用绝对路径或结合os模块找到脚本的根目录来构建路径。可以使用os.path.abspath(__file__)获取当前脚本的绝对路径,然后构建目标路径。确保以'w'模式打开文件用于写入,而非试图读取。
摘要由CSDN通过智能技术生成

我想在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)利润(修复路径)

@好吧,问题是文件根本没有被创建,这就是我发布的原因。所以我会一个人跟随你的脚步,

所以您提供了一个无效的路径。你一定能想出一个有效的(测试用的),对吧?

路径无效吗?当然是。

您说您想创建一个文件,但实际上您正在读取一个文件,该文件是在w中打开的(写入模式)。那你想怎么办?

为什么要以写模式打开一个文件,然后尝试从中读取?

@Burhankhalid-您能在stackoverflow.com/questions/20870603/…上提供您的建议吗?

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

如果你不知道这条绝对路径,请参考这个问题。

python不解释../,它将在cwd中查找名为".."的目录。

您或者必须硬编码路径:

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

content = json.load(file_handler)

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

python:如何查找脚本的目录

在python中,如何获取当前正在执行的文件的路径和名称?

编辑:我错了,python解释了"…",这里发生的是,这是开始,是CWD而不是你的脚本。

$ echo 'Hello world' > text_file.txt

$ mkdir test/

$ cd test

$ python

[...]

>>> open('../text_file.txt').read()

'Hello world

'

对于下列项目结构:

.

`-- 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 ##

您可以这样做:从该路径中查找当前文件路径和脚本目录路径

dir = os.path.dirname(__file__)

然后,您可以添加或加入您想在其中分别创建文件的路径。

jsonfilepath ="../../../../../service/constants/global_variables.json"

reljsonfilepath = os.path.join(dir, jsonfilepath)

f = open (reljsonfilepath, 'w')

请检查,因为这是未测试的代码。

您确定路径正确吗?

假设当前路径为:../myproject/gui/modules/energy/配置

您提到的路径是:

"..(a)/..(b)/..(c)/..(d)/..(e)/service/constants/global_variables.json"

(a) = energy/

(b) = modules/

(c) = gui/

(d) = myproject/

(e) = ../

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值