我正在嘗試在特定檔案夾中創建一個新的空 csv 檔案。我只能通過兩個步驟來完成它。我的代碼如下。
#Step 1:Create a new csv file in the current folder.
import csv
csv_name='persons.csv'
with open(csv_name, 'wb') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
#Step 2:Move the new csv file from the current folder to the target folder.
csv_name='persons.csv'
import shutil
path_2=r'C:\Users\jennyhsiao\Technology'
original =path_2 x csv_name "'"
target = r'C:\Users\jennyhsiao\OneDrive'
shutil.move(original, target)
雖然代碼可以成功創建新的 csv 檔案并將其移動到目標檔案夾。但是,我想用更少的步驟而不是兩個步驟來完成。有沒有辦法做到這一點?
uj5u.com熱心網友回復:
import os
fileName = "csvFileName.csv"
filePath = "path/to/folder"
path = os.path.join(filePath, fileName)
with open(path, "w") as csvFile:
#Do what you want.....
這將正常作業(:
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/483582.html
