code:
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 17 18:34:36 2023
@author: SavingKing
"""
folder = r"C:\Temp"
fname = "test.txt"
import os
fpath = os.path.join(folder,fname)
#不需要[ ] 包覆folder,fname
fpath1 = "\\".join([folder,fname])
#需要[ ] 包覆folder,fname
print("fpath:",fpath)
print("fpath1:",fpath1)
fpath2 = "\\".join(folder,fname) #TypeError
#沒有用[ ] 包覆folder,fname,會觸發TypeError
#TypeError: str.join() takes exactly one argument (2 given)
輸出結果:
字串串接: folder + "\\" + fname
同效果
反過來講,
如何將file_path拆分為
folder_path & file_name?
接續以上的程式:
推薦hahow線上學習python: https://igrape.net/30afN
近期留言