首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当我尝试替换文件时,为什么访问被拒绝

当我尝试替换文件时,为什么访问被拒绝
EN

Stack Overflow用户
提问于 2019-06-27 02:55:04
回答 1查看 110关注 0票数 0

我试图通过基本上复制一个名为" test.csv“的文件到"new.csv”来替换一个文件,但是它找不到test.csv,即使它在相同的工作目录中。

代码语言:javascript
复制
    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。

EN

回答 1

Stack Overflow用户

发布于 2019-06-27 02:59:58

它们位于同一目录中,但您尚未指示Python进行检查:

代码语言:javascript
复制
import os
base_path = "D:\\johnp\\kivy_venv\\betaapp2\\"

with open(os.path.join(base_path, "test.csv")) as infile:
    with open(os.path.join(base_path, "new.csv"), 'w') as outfile:

如果没有path,Python只会查看当前的工作目录。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56779392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档