[英]How to create a csv file in a specific folder in Python?
我正在嘗試在特定文件夾中創建一個新的空 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 文件並將其移動到目標文件夾。 但是,我想用更少的步驟而不是兩個步驟來完成。 有什么辦法嗎?
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.....
這將正常工作(:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.