我目前正在工作的CSV文件创建编辑和附加到CSV文件的问题是,我正在尝试更新到现有的CSV文件,但遇到了这样做的麻烦。在
当我运行代码时,我得到以下错误。在/usr/bin/python3.5 /home/usr/PycharmProjects/csv/csv_file.py
2016-11-12 00:04:03.975247
2016-11-11 23:59:17.591426
MOD TIME IS Different
Traceback (most recent call last):
File "/home/usr/PycharmProjects/csv/csv_file.py", line 157, in
Direcotry_function(sys.argv)
File "/home/usr/PycharmProjects/csv/csv_file.py", line 133, in Direcotry_function
for row in reader:
File "/usr/lib/python3.5/csv.py", line 109, in __next__
self.fieldnames
File "/usr/lib/python3.5/csv.py", line 96, in fieldnames
self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
Process finished with exit code 1
下面是我似乎无法运行的Python代码。在
^{pr2}$
编辑:
所以,在玩弄了代码之后:我在做这件事之后,我要面对的问题是没有阅读整个代码日期时间.csv如果我更改了一个文件中的某些内容,我就会得到这个
它不会给我测试温度csv在
文件名,修改日期
/主页/usr/Desktop/test232016-11-12 11:22:07.410170
文件名,修改日期
/主页/usr/Desktop/test232016-11-12 11:22:07.410170
文件名,修改日期
/主页/usr/Desktop/test232016-11-12 11:22:07.410170def Direcotry_function(directory_dest):
global directory_path #call the Global Variable
directory_path = os.getcwd() # getting Current Directory
if len(directory_dest) > 1:
directory_path = directory_dest
for list in filepath_list:
Originalfiles = ''.join(list)#Convert to Str
drive, path = os.path.splitdrive(Originalfiles)#Get the Path
path, filename = os.path.split(path)#Split the path and filename
makedir = path + '/' + filename
Originalfiles_Timestamp = os.path.getmtime(Originalfiles)#Get the Timestamp of File in seconds
mTime = datetime.datetime.fromtimestamp(Originalfiles_Timestamp)#Get the Date time in Month Yearformat
print(mTime)
print(filename)
if os.path.exists(os.path.join(directory_path, filename[:]+".copy")):#Check if the FIle exists
file_csv = csv.DictReader(open('datetime.csv'))#open CSV FILE
for row in file_csv:#Loop over
if row['FileName'] == Originalfiles:
rowTime = datetime.datetime.strptime(row['ModificationDate'], "%Y-%m-%d %H:%M:%S.%f") #Convert row to DateTime
if rowTime == mTime: #Check if Time inside CSV equals mTime
print("same")
else:
with open('datetime.csv', "r") as f:
reader = csv.DictReader(f)
fieldnames = ['FileName', 'ModificationDate'] # Create Dict Keys
for row in reader:
with open("temp.csv", 'w') as g:
writer = csv.DictWriter(g, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({"FileName": Originalfiles, "ModificationDate": mTime})
#shutil.move("temp.csv", 'datetime.csv')
os.system('cp -rv ' + makedir + ' ' + directory_path + '/' + filename[:] + ".copy")
else:
#If Copy File Does not Exists in the Folder do the Following:
os.system('cp -r ' + makedir + ' ' + directory_path + '/' + filename[:]+".copy")#Copy ther File to the Directory
with open('datetime.csv', 'a') as csvfile:#Open CSV File
fieldnames = ['FileName', 'ModificationDate']#Create Dict Keys
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'FileName': Originalfiles, 'ModificationDate': mTime})#Write to The CSV File