utime csv文件提供双反斜杠

时间:2018-07-20 15:27:37

标签: python

我现在有以下代码:

import os
import time
import datetime
import csv

year = 2017
month = 12
day = 5
hour = 19
minute = 50
second = 0

date = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
modTime = time.mktime(date.timetuple())
with open('Book1.csv', newline='') as csvfile:
     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
     for row in spamreader:
         rows = ''.join(row)

         os.utime(rows, (modTime, modTime))


print("done")

当我打印行时,它可以正常工作并打印c:\test

当我运行utime时,它说找不到合理的c:\\test

在csv文件中,它只显示C:\

我该如何解决,这样它只能产生一个反斜杠。

1 个答案:

答案 0 :(得分:0)

从OS导入的第一个路径

from os import path

然后使用路径分割行字符串

result = path.split(rows)

将path.split结果的第一部分设置为os.chdir [0]

os.chdir(result[0])

最后os.utime chdir中的文件,其文件名为result [1]

os.utime(result[1], (modTime, modTime))

这是最终代码

import os
import time
import datetime
import csv
from os import path

year = 2017
month = 12
day = 5
hour = 19
minute = 50
second = 0

date = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute, 
second=second)
modTime = time.mktime(date.timetuple())
with open('Book1.csv', newline='') as csvfile:
 spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
 for row in spamreader:
     rows = ''.join(row)
     result = path.split(rows)
     os.chdir(result[0])
     os.utime(result[1], (modTime, modTime))



print("done")