import os
current_dir = os.path.dirname(os.path.abspath(__file__))
print("current_dir:",current_dir)
print("abspath:",os.path.abspath(__file__))
print("__file__:",__file__)

os.path.abspath(__file__)
用于获取当前脚本的绝对路径,
os.path.dirname()
用于获取文件所在文件夹的路径。
这个代码将打印出当前.py文件
所在的文件夹路径。
就Spyder輸出結果來看,
不需要os.path.abspath()
__file__ 已經是指定到py檔的絕對路徑
在Spyder的IPython控制台中運行代碼時,
__file__變量確實已被定義,
並且包含當前運行的Python腳本的絕對路徑。
因此,直接使用__file__變量
即可獲取當前腳本的絕對路徑。
而在某些情況下,
如果你的腳本是在其他地方引用的,
那麼使用os.path.abspath()
可以確保始終獲取腳本的絕對路徑。
current_dir = os.getcwd() :

“cwd” 是 “current working directory” 的縮寫,
指的是當前工作目錄。
在文件路径中使用”./”表示当前目录。
例如,如果你当前的工作目录是”/home/user”,
那么”./file.txt”将指向”/home/user/file.txt”。

推薦hahow線上學習python: https://igrape.net/30afN
在计算机术语中,cwd 和 pwd 是常见的缩写,
代表与目录相关的功能:
cwd – 全称是 Current Working Directory。
这表示当前的工作目录,
即当前命令行或程序正在访问的目录。
pwd – 全称是 Print Working Directory。
这是一个常用的命令,
特别是在 Unix、Linux、或类 Unix 系统的命令行中,
用来显示当前工作目录的完整路径。


![Python如何寫入docx文件? from docx import Document ; doc = Document() ; table = doc.add_table(rows=5, cols=3) ; table.cell(r,c).text = str(tabs[r][c]) ; doc.add_heading ; p = doc.add_paragraph ; p.add_run ; doc.add_picture ; 使用wordPad開啟會少最後一個row,可以用免費的LibreOffice Python如何寫入docx文件? from docx import Document ; doc = Document() ; table = doc.add_table(rows=5, cols=3) ; table.cell(r,c).text = str(tabs[r][c]) ; doc.add_heading ; p = doc.add_paragraph ; p.add_run ; doc.add_picture ; 使用wordPad開啟會少最後一個row,可以用免費的LibreOffice](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2022/09/20220914154313_30.jpg?quality=90&zoom=2&ssl=1&resize=350%2C233)





![Python, typing: 函數庫規格標註; def addTest(x:float, y:float) -> float: List[資料型態] Set[資料型態] Tuple[資料型態] Dict[str,value的資料型態] Union[資料型態1, 資料型態2] ,函式若有多個輸出值,其實是輸出一個tuple Python, typing: 函數庫規格標註; def addTest(x:float, y:float) -> float: List[資料型態] Set[資料型態] Tuple[資料型態] Dict[str,value的資料型態] Union[資料型態1, 資料型態2] ,函式若有多個輸出值,其實是輸出一個tuple](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2022/09/20220907154601_86.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

近期留言