python在其中创建文件

2024-05-14 08:13:03 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中创建文件时,使用以下命令:

NewFile = open('pactice1.txt','w')

它是在哪个目录中创建的?你知道吗


Tags: 文件命令目录txtopennewfilepactice1
3条回答

您可以使用找到(会话或脚本的)当前工作目录

import os
print(os.getcwd())

如果NewFile仍然存在(即使关闭)

os.path.abspath(NewFile.name)

提供文件的完整路径。你知道吗

Document

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)¶

Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.

file is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.)

在执行此脚本的同一目录中。你知道吗

如果您不是从命令行运行这个,那么请尝试使用“os”模块中的方法打印当前工作目录。你知道吗

相关问题 更多 >

    热门问题