python中的文件没有任何理由是空的 [英] Files in python are empty for no reason

查看:172
本文介绍了python中的文件没有任何理由是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把我的世代从我的实际游戏中分离出来,因为我经常失败。但由于某种原因,它坚持文件是空的/从它获得的变量是空的,有时,当我看后,实际的程序文件已经清空文本文件的所有信息,有时不是。以下是代码:

Dropbox主代码



Dropbox World的发现



以下是主要代码中文件处理的一小部分:

 world_file = open(C:\ Users \Ben\Documents\Python Files\PlatformerGame Files\World.txt,r)
world_file_contents = world_file.read()
world_file.close()
world_file_contents = world_file_contents.split(\ n)
WORLD = []
为world_file_contents中的数据:
useful_data = data.split(:)
WORLD.append(Tile(usable_data [0],usable_data [1]))

和tile类:

  class Tile():
def __init __(self,location,surface):
self.location = location
self.surface = sur面对

和错误:

<$ p $ (< code> Traceback(最近一次调用的最后一个):
< module>文件C:\ Users \Ben\Documents\Python Files\PlatformerGame
Game .__ main __()
在__main__
文件C:\ Users \Ben\Documents\Python Files\PlatformerGame中,第42行WORLD.append(Tile可用数据[0],可用数据[1]))
IndexError:列表索引超出范围

很抱歉,如果这是明显的。也可以使用pygame。

解决方案

输入文件中可能有空行;你可以跳过这些。



你也可以简化你的阅读代码:

 打开(C:\ Users \Ben\Documents\Python Files\PlatformerGame Files\World.txt,r)as world_file:
WORLD = [Tile (* line.strip()。split(:,1))for line in world_file if':'in line]

如果在它们中有一个字符,它只处理行,只拆分一次,然后创建 WORLD b
$ b

至于使用 os.startfile():你正在启动其他脚本在后台。然后,该脚本将打开文件以写入并明确地清空文件,然后生成新的数据。 在同一时间,您正试图从该文件读取。当时你最终可能读到一个空的文件,因为另一个进程还没有完成生成和写入数据,而当文件写入被缓冲时,你将不会看到所有的文件直到其他进程关闭文件并退出。



不要使用 os.startfile() 在这里。导入其他文件,而不是;那么代码将在导入过程中执行 ,并保证文件被关闭。


I have tried to split up my world generation from my actual game, since I usually fail with it. But for some reason it keeps insisting the file is empty/ the variable gained from it is empty, and sometimes, when I look afterwards, the actual program file has emptied the text file with all the info, sometimes not. Here is the code:

Dropbox Main code

Dropbox World gen

Here's a small extract of just the file handling in the main code:

world_file = open("C:\Users\Ben\Documents\Python Files\PlatformerGame Files\World.txt", "r")
world_file_contents = world_file.read()
world_file.close()
world_file_contents = world_file_contents.split("\n")
WORLD = []
for data in world_file_contents:
    usable_data = data.split(":")
    WORLD.append(Tile(usable_data[0],usable_data[1]))

And the tile class:

class Tile():
    def __init__(self,location,surface):
        self.location = location
        self.surface = surface

And the error:

Traceback (most recent call last):
  File "C:\Users\Ben\Documents\Python Files\PlatformerGame", line 89, in <module>
    Game.__main__()
  File "C:\Users\Ben\Documents\Python Files\PlatformerGame", line 42, in __main__
    WORLD.append(Tile(usable_data[0],usable_data[1]))
IndexError: list index out of range

Sorry if it's obvious. Also I'm using pygame.

解决方案

You probably have empty lines in your input file; you would want to skip these.

You can also simplify your tile reading code:

with open("C:\Users\Ben\Documents\Python Files\PlatformerGame Files\World.txt", "r") as world_file:
    WORLD = [Tile(*line.strip().split(":", 1)) for line in world_file if ':' in line]

This only processes lines if there is a : character in them, splits only once, and creates the WORLD list in one loop.

As for using os.startfile(): you are starting the other script in the background. That script then opens the file to write to and explictly empties the file, before it generates new data. At the same time you are trying to read from that file. Chances are you end up reading an empty file at that time, as the other process hasn't yet finished generating and writing the data, and as file writes are buffered you won't see all of the data until the other process closes the file and exits.

Don't use os.startfile() at all here. Import the other file instead; then the code will be executed during import and the file is guaranteed to be closed.

这篇关于python中的文件没有任何理由是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆