我试图通过基本上复制一个名为" test.csv“的文件到"new.csv”来替换一个文件,但是它找不到test.csv,即使它在相同的工作目录中。
def cop(self):
with open('D:\\johnp\\kivy_venv\\betaapp2\\test.csv') as infile:
with open('D:\\johnp\\kivy_venv\\betaapp2\\new.csv', 'w') as outfile:
for line in infile:
# do things
outfile.write(line)
os.replace('D:\\johnp\\kivy_venv\\betaapp2\\new.csv', 'D:\\johnp\\kivy_venv\\betaapp2\\test.csv')
def sign_in(self, text_input):
self.text_input = text_input
count = 0
h = ""
d = ""
with open('D:\\johnp\\kivy_venv\\betaapp2\\test.csv', 'r') as fh:
reader = csv.reader(fh)
# get the headers out first, which is the first line
headers = next(reader)
for line in reader:
# this will make a dictionary with the header values as
# keys and the line entries as values
entry = dict(zip(headers, (l.strip() for l in line)))
# use key access, it makes the code a bit more readable
if entry['Name'] == text_input.strip():
if entry['Position'] == "Vice President":
line[8] = float(line[8]) + 3.5
self.cop()
self.signin()
else:
self.noUser()
应该通过运行sign_in然后将其复制到new.csv来更新test.csv。然后用new.csv替换test.csv。
https://stackoverflow.com/questions/56779392
复制相似问题