如何在 Python 的特定文件夹中创建 csv 文件?
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.....
这会很好用(:
我正在尝试在特定文件夹中创建一个新的空 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.....
这会很好用(: