首页 > 解决方案 > 如何指定CSV保存在python中的路径

问题描述

默认路径似乎是我正在使用的项目文件夹,但我找不到任何文档来说明如何指定 csv 在机器上的保存位置,我目前正在使用 shutil 将文件移动到我想要的路径但是这个不是一个强大的解决方案,因为此应用程序将被许多用户使用,这可能会导致问题。

length = len(list1)

#convert job name to csv name
jobname = jobname + ".csv"


with open (jobname, 'w', newline='') as f:
    thewriter = csv.writer(f)

    thewriter.writerow(column_header_list)
    
    i = 0
    for x in range(length):
        thewriter.writerow([list1[i],0,0,0,0,0,0,0,0,0,0])
        i += 1

#create a path for the newly made file
original_path = r'C:\Users\Scott\OneDrive\Documents\DL\Main Application MRL'
original_path = (original_path + '\\') #concat to add backslash to path 
original_path = original_path + jobname #add jobname to path 

target_path = r'C:\Users\Scott\OneDrive\Documents\DL\JobData'

shutil.move(original_path,target_path)

标签: pythoncsv

解决方案


推荐阅读