0
我使用以下python腳本將MySQL錶轉儲爲CSV文件。但它保存在python腳本保存的同一個文件夾中。我想將它保存在另一個文件夾中。我該怎麼做?謝謝將MySQL錶轉儲爲CSV文件並使用Python腳本將其保存到指定位置
print 'Writing database to csv file'
import MySQLdb
import csv
import time
import datetime
import os
currentDate=datetime.datetime.now().date()
user = ''
passwd = ''
host = ''
db = ''
table = ''
con = MySQLdb.connect(user=user, passwd=passwd, host=host, db=db)
cursor = con.cursor()
query = "SELECT * FROM %s;" % table
cursor.execute(query)
with open('Data on %s.csv' % currentDate ,'w') as f:
writer = csv.writer(f)
for row in cursor.fetchall():
writer.writerow(row)
print 'Done'