简体   繁体   English

我无法读取我刚刚在 python 中写入的文件

[英]I can't read the file i just write in python

I write a script to remove spaces before and after each data, such as 1,2, 3, 4 will become 1,2,3,4.我写了一个脚本,把每个数据前后的空格去掉,比如1,2,3,4会变成1,2,3,4。 My code is successful,the object file is true(By opening it on the computer, the result is completely correct).我的代码是成功的,object文件是真的(在电脑上打开,结果完全正确)。 After it, I did a test,in this test,I try to read the object file by python,the result is none.之后,我做了一个测试,在这个测试中,我尝试通过 python 读取 object 文件,结果是无。 I don't know why.我不知道为什么。 Here is my code below这是我下面的代码

with open("D:\\Users\\Documents\\Documents\\pythonFile\\PracticeAboutPython123\\resource\\data.csv","r+") as fi,\        open("D:\\Users\\Documents\\Documents\\pythonFile\\PracticeAboutPython123\\resource\\clear_data.csv","w+") as fo:
    fi_reader=fi.readlines()  
    for line in fi_reader:
        line=line.replace(" ","")
        fo.write(line)  
    #Here's where things start to go wrong
    fo_reader=fo.read()
    print("the result of fo is here:",fo_reader)

Origin file原始文件

1,2,3,4,5,6,7
8, 3, 2, 7, 1, 4, 6, 5
6, 1, 3, 8, 5, 7, 4, 2
'a','b','c','x','y','z','i','j','k'
'k', 'b', 'j', 'c', 'i', 'y', 'z', 'a', 'x'
'z', 'c', 'b', 'a', 'k', 'i', 'j', 'y', 'x'
'a', 'y', 'b', 'x', 'z', 'c', 'i', 'j', 'k'
5, 2, 4, 7, 1, 6, 8, 3

My object file is entirely right我的 object 文件完全正确

1,2,3,4,5,6,7
8,3,2,7,1,4,6,5
6,1,3,8,5,7,4,2
'a','b','c','x','y','z','i','j','k'
'k','b','j','c','i','y','z','a','x'
'z','c','b','a','k','i','j','y','x'
'a','y','b','x','z','c','i','j','k'
5,2,4,7,1,6,8,3

But the print is an error但是打印是错误的

the result of fo is here:  (There should have some result, but it's nothing)

Try closing and reopening the file or use seek to move to the front of the file.尝试关闭并重新打开文件或使用 seek 移动到文件的前面。 I would assume python is trying to read from the current Cursor position which would be where it finished writing.我假设 python 正在尝试从当前的 Cursor position 中读取,这将是它完成写入的地方。

Check out Section 7.2.1 Method of File Objects in the python documentation: Python 7.2.1 Methods of File Objects查看 python 文档中的第 7.2.1 节文件对象的方法: Python 7.2.1 文件对象的方法

You have given read permission to one file and the other file has write permission so you can't write and then read the fo file for example I have given read permission to the MyFile_1.txt file您已授予一个文件的读取权限,而另一个文件具有写入权限,因此您无法写入然后读取 fo 文件,例如我已授予 MyFile_1.txt 文件的读取权限

with open("MyFile_1.txt","r") as ReadText:
   print(ReadText.read()) # This is Working
   ReadText.write("My Blabla"); # Not Working Because I only gave him readability "r+" or "r"

Anyway your code will be something like this无论如何,您的代码将是这样的

with open("C:/Users/Root/Desktop/1.txt","r+") as fo:
fi_reader=fo.readlines()
with open("C:/Users/Root/Desktop/2.txt","w+") as fi:
    for line in fi_reader:
        fi.write(line.replace(" ",""))

with open("C:/Users/Root/Desktop/2.txt","w+") as fi: print(fo.read())使用 open("C:/Users/Root/Desktop/2.txt","w+") 作为 fi: print(fo.read())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM