我正在学习Python,不过遇到一些问题,想请教
OS模块中的os.path.dirname(__file__)和os.path.abspath(__file__)
运行os.path.dirname(__file__)时候,为什么返回的是空白呢?是不是因为他运行的是相对路径???
如果是的话:
1:我怎么能够知道,括号内的文件是以相对路径还是绝对路径被运行的?
2:为什么我运行下面例子脚本的时候,这个文件是以相对路径被运行的呢?
比如我下面的例子
import os
print (os.path.dirname(__file__))
print (os.path.abspath(__file__))
print (os.path.abspath(os.path.dirname(__file__)))
print (os.path.dirname(os.path.abspath(__file__)))
PS:附加问题
os.path.abspath(os.path.dirname(__file__))和os.path.dirname(os.path.abspath(__file__))性质是否一样呢?
建議你可以稍微瀏覽一下 Python doc: os.path, 你就會明白囉:
我放上跟你問題相關的幾個條目:
os.path.abspath(path)
Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).
os.path.normpath(path)
Normalize a pathname by collapsing redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B. This string manipulation may change the meaning of a path that contains symbolic links. On Windows, it converts forward slashes to backward slashes. To normalize case, use normcase().
os.path.dirname(path)
Return the directory name of pathname path. This is the first element of the pair returned by passing path to the function split().
os.path.split(path)
Split the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that. The tail part will never contain a slash; if path ends in a slash, tail will be empty. If there is no slash in path, head will be empty. If path is empty, both head and tail are empty. Trailing slashes are stripped from head unless it is the root (one or more slashes only). In all cases, join(head, tail) returns a path to the same location as path (but the strings may differ). Also see the functions dirname() and basename().
我們做以下觀察:
test.py
運行:
結果:
首先
__file__
的值其實就是在命令列上 invoke Python 時給的 script 名稱:在這裡, 因為
, 所以__file__
的值為test.py
, 所以print(__file__)
的結果是test.py
的值為print(__file__)
的結果是也就不意外了。
接著,os.path.dirname(__file__)
之所以得出空白(空字串), 是因為__file__
就只是一個單純的名稱(非路徑) 且dirname
也只是很單純的利用os.path.split()
os.path.dirname(__file__)
之所以得出空白(空字串), 是因為就只是一個單純的名稱(非路徑) 且
我分會發現切出來的dirname
也只是很單純的利用os.path.split()
來切割這個名稱(這當然沒甚麼好切的, 連路徑分割符都沒有):head
是空字串, 所以dirname
的結果是空白。
abspath
動用了os.getcwd()
所以即便給定的是單純的名稱, 也能返回路徑:
而os.path.abspath(os.path.dirname(__file__))
的結果就等於是os.getcwd()
的結果去接上dirname
得到的空字串:
最後,os.path.dirname(os.path.abspath(__file__))
的結果是這麼來的:
🎜希望講到這裡有讓你明白!🎜Conclusion
Now briefly answer your question
Why is
dirname
blank?Because you gave a simple name when running, so
__file__
is a simple name and not a pathHow can I know whether the file in the brackets is run with a relative path or an absolute path?
It’s very simple, it depends on how you run Python
Why is this file run with a relative path when I run the example script below?
Because
$ python 1.py
you gave the relative path yourselfos.path.abspath(os.path.dirname(__file__))
和os.path.dirname(os.path.abspath(__file__))
Are the properties the same?Basically the same
Questions I answered: Python-QA