简体   繁体   English

本地相对文件名在Python中意味着什么?

[英]What does local relative filename means in Python?

I found, that to proceess files given by filename I need to do 我发现,要处理文件名给定的文件,我需要做

import os
dir = os.path.dirname(__file__)
filename = os.path.join(dir, '/relative/path/to/file/you/want')

or 要么

import os
dir = os.getcwd()
filename = os.path.join(dir, '/relative/path/to/file/you/want')

But if I do 但是如果我这样做

filename = 'myfile.txt'

then where it will be look for this file? 那么将在哪里寻找该文件?

Short answer: in the current working directory. 简短的答案:在当前工作目录中。

Long answer: 长答案:

From https://docs.python.org/2/library/stdtypes.html#bltin-file-objects 来自https://docs.python.org/2/library/stdtypes.html#bltin-file-objects

File objects are implemented using C's stdio package 文件对象是使用C的stdio包实现的

From https://docs.python.org/2/library/functions.html#open https://docs.python.org/2/library/functions.html#open

The first two arguments are the same as for stdio's fopen(): name is the file name to be opened 前两个参数与stdio的fopen()相同:name是要打开的文件名

So, this is answered by looking at documentation for stdio : 因此,这可以通过查看stdio文档来解决:

From http://www.cplusplus.com/reference/cstdio/fopen/ http://www.cplusplus.com/reference/cstdio/fopen/

filename

C string containing the name of the file to be opened. C字符串,包含要打开的文件的名称。

Its value shall follow the file name specifications of the running environment and can include a path (if supported by the system). 其值应遵循运行环境的文件名规范,并且可以包含路径(如果系统支持)。

"Specifications of the running environment" means that it will interpret it as if you typed the path into where you were running the file from, aka, cwd. “正在运行的环境的规范”意味着它将解释该环境,就像您键入从中(又名cwd)运行文件的路径一样。

For example, if I have a script located in ~/Desktop/temp.py that reads: 例如,如果我有一个位于~/Desktop/temp.py中的脚本,其内容为:

f = open("log.txt", 'r')
print "success opening"
f.close()

and I have a file located at ~/Desktop/log.txt , I get the following output: 并且我有一个文件位于~/Desktop/log.txt ,我得到以下输出:

~/Desktop $ python temp.py
success opening

But if I cd .. and try again: 但是,如果我cd ..然后重试:

~ $ python ~/Desktop/temp.py
Traceback (most recent call last):
  File "/home/whrrgarbl/Desktop/temp.py", line 1, in <module>
    f = open("log.txt", 'r')
IOError: [Errno 2] No such file or directory: 'log.txt'

Just to verify: 只是为了验证:

~ $ touch log.txt
~ $ python ~/Desktop/temp.py
success opening

So you can see it is trying to open it relative to the directory I ran the script from, not the directory in which the script is located. 因此,您可以看到它正在尝试相对于我运行脚本的目录(而不是脚本所在的目录)打开它。

Created a simple python script ( open.py ) and ran it with strace . 创建了一个简单的python脚本( open.py )并使用strace运行它。

The script: 剧本:

#!/usr/bin/env python

with open('myfile.txt', 'r') as fd:
    pass

Strace command: strace ./open.py strace ./open.py命令: strace ./open.py

This showed me (showing only relevant portion): 这显示给我(仅显示相关部分):

read(3, "#!/usr/bin/env python\n\nwith  ope"..., 4096) = 70
read(3, "", 4096)                       = 0
brk(0x23a2000)                          = 0x23a2000
close(3)                                = 0
munmap(0x7f7f97dc3000, 4096)            = 0
open("myfile.txt", O_RDONLY)            = -1 ENOENT (No such file or directory)
write(2, "Traceback (most recent call last"..., 35Traceback (most recent call last):
) = 35
write(2, "  File \"./open.py\", line 3, in <"..., 40  File "./open.py", line 3, in <module>
) = 40

Looking at the open system call got the path as it was provided in the python script. 查看open系统调用即可获得python脚本中提供的路径。 open will try to open the file in current working directory, which is where the program was run from. open将尝试在当前工作目录中打开该文件,该文件是运行该程序的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM