Python 哈希保存文件句柄

发布于 2024-10-22 06:14:02 字数 278 浏览 1 评论 0原文

因此,我正在编写一个脚本,该脚本在运行时会自动从 IRC XDCC 机器人下载某些文件。这些请求是异步的,并且可能有不同的数量,具体取决于配置文件,因此我想将文件句柄保留在哈希表或库中,以便可以根据文件发送者是谁以及他们发送的文件轻松引用它们(在触发事件期间读取)。 Python 抱怨说 SyntaxError: can't allocate to function call 所以我猜它不会按照我想要的方式工作。

有更简单的方法来做到这一点吗?我是不是找错树了?

谢谢! -拉塞尔

So I'm working on a script that will automatically download certain files from IRC XDCC bots when run. These requests are asynchronous and there can be a varying number, depending on a config file so I wanted to keep the file handles in a hash table or library so they could easily be referenced based on who the file sender was and the file they are sending (read during a triggered event). Python is complaining saying SyntaxError: can't assign to function call so I'm guessing it won't work quite how I want.

Any easier way to do this? Am I barking up the wrong tree here?

Thanks! -Russell

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏の忆 2024-10-29 06:14:02

问题是赋值语句的左侧必须是左值,编译器知道它具有内存地址,就像变量一样。在其他编程语言中也是如此。函数的返回值是一个右值,或者一个值。

这些是其他非法分配:

f() = 1
2 = 1
None = 0
[1,2] = []

请注意,以下内容在语法上是正确的,因为编译器知道如何计算要分配的内存位置的地址:

f().a = None
[1,2][0] = 0

The problem is that the left side of an assignment statement must be an lvalue, that is something that the compiler knows has a memory address, like a variable. It is the same in other programming languages. The return value of a function is an rvalue, or a pure value.

These are other illegal assignments:

f() = 1
2 = 1
None = 0
[1,2] = []

Note that the follwing are syntactically correct because the compiler knows how to compute an address for the memory location to be assigned:

f().a = None
[1,2][0] = 0
南街女流氓 2024-10-29 06:14:02

创建一个空哈希:

files = {}

将项目添加到哈希:

files["gin"] = open('ginpachi.txt','w')
files["ahq"] = open('ahq[DaBomb].txt','w')

像普通文件处理程序一样引用它们

files["gin"].close( )
...

不幸的是,网络上没有任何关于此的信息(特别是哈希值和文件句柄)。
案件已结

Create an empty hash:

files = {}

Add items to the hash:

files["gin"] = open('ginpachi.txt','w')
files["ahq"] = open('ahq[DaBomb].txt','w')

Reference them like you would a normal file handler

files["gin"].close()
...

Unfortunately, there wasn't any information on this on the web (specifically with hashes and file handles).
Case closed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文