无法从python中的文件中读取数据

时间:2015-07-06 11:33:44

标签: python

我无法从python中读取文件中的数据。下面是我得到的示例代码和错误。

- (void)applicationDidEnterBackground:(UIApplication *)application {
     // pause the video here
 }

- (void)applicationWillEnterForeground:(UIApplication *)application      {
    // resume video when screen unlock 
 }

在读取文件(anc.txt)时我收到错误:TypeError:参数1必须是字符串或只读字符缓冲区,而不是文件。我无法从文件(abc.txt)中读取last_Exe_date中的值。如果我错了代码,请你纠正我。

2 个答案:

答案 0 :(得分:4)

当您读取文件一次时,光标位于文件的末尾,您不会通过重新阅读它来获得更多信息。仔细阅读the docs以了解更多信息。并使用readline逐行读取文件。

哦,并在read来电结束时删除分号...

答案 1 :(得分:0)

以下应该可以正常工作:

f = open("/opt/test/abc.txt","r")
last_Exe_date = f.read()
f.close()

如上所述,您有f.read()两次,所以当您尝试将内容存储到last_Exe_date时,它将为空。

您还可以考虑使用以下方法:

with open("/opt/test/abc.txt","r") as f:
    last_Exe_date = f.read()

这将确保文件随后自动关闭。