小编Fra*_*sco的帖子

向python生成器添加元素

是否可以将元素附加到python生成器?

我目前正在尝试从一组无组织的文件夹中获取所有图像,并将它们写入新目录.为了获取文件,我使用的是os.walk(),它返回一个目录中的图像文件列表.虽然我可以从这个单独的列表中创建一个生成器,但我不知道如何将所有这些列表组合到一个单独的生成器中.任何帮助将非常感激.

有关:

python append generator

17
推荐指数
3
解决办法
2万
查看次数

Vim无法保存到python创建的临时文件

目标

我正在尝试在vim中创建和编辑临时文件(与git/hg/svn中的提交脚本完全相同).

目前的代码

我在这个答案中找到了一个方法: 从python脚本中调用EDITOR(vim)

import sys, tempfile, os
from subprocess import call
EDITOR = os.environ.get('EDITOR','vim')
initial_message = "write message here:"
with tempfile.NamedTemporaryFile(suffix=".tmp") as tmp:
  tmp.write(initial_message)
  tmp.flush()
  call([EDITOR, tmp.name])
  tmp.seek(0)
  print tmp.read()
Run Code Online (Sandbox Code Playgroud)

问题

当我运行上面的代码时,tempfile不会读取在vim中所做的更改.这是我在vim中添加其他几行之后的输出:

fgimenez@dn0a22805f> ./note.py
Please edit the file:

fgimenez@dn0a22805f>
Run Code Online (Sandbox Code Playgroud)

现在为有趣(奇怪)的部分.如果我将编辑器更改为nano或emacs,则脚本可以正常工作!到目前为止,当我使用vim或textedit时,这似乎只会破坏.

作为另一个实验,我尝试连续调用几个编辑器来查看会发生什么.修改后的代码是:

with tempfile.NamedTemporaryFile(suffix=".tmp") as tmp:
  tmp.write(initial_message)
  tmp.flush()
  # CALLING TWO EDITORS HERE, VIM THEN NANO
  call(['vim', tmp.name])
  raw_input("pausing between editors, just press enter")
  call(['nano', tmp.name])
  tmp.seek(0)
  print tmp.read()
Run Code Online (Sandbox Code Playgroud)

即我用vim然后nano编辑.会发生什么是nano DOES注册vim所做的更改,但是python没有注册任何内容(与之前相同的结果):

fgimenez@dn0a22805f> ./note.py
Please edit the file: …
Run Code Online (Sandbox Code Playgroud)

python vim temporary-files

5
推荐指数
1
解决办法
367
查看次数

标签 统计

python ×2

append ×1

generator ×1

temporary-files ×1

vim ×1