python-3.x - 映射到 os.rename 多个文件
问题描述
我有这样的程序
#+begin_src ipython :session alinbx :results output
import os
import glob
import copy
fs = os.listdir()
fs = filter(lambda x: not x.endswith("org"), fs)
fsc = copy.deepcopy(fs)
print(list(fsc)[:5])
# map(lambda x: os.rename(x, f"{x}.org"), fs)
#+end_src
#+RESULTS:
: ['19.Pseudo-Terminals', '12.Thread-Control', '05.Standard-IO-Library', '07.Process-Environment', '03.File-IO']
稍后,将 org 添加到名称中
#+begin_src ipython :session alinbx :results output
map(lambda x: os.rename(x, x+'.org'), fs)
! ls | head -n 5
#+end_src
#+RESULTS:
: 00.Preface.org
: 01.UNIX-System-Overvie
: 01.xhtml
: 02.UNIX-Standardization-and-Implementations
: 03.File-IO
它不起作用,因此 for 循环有效
#+begin_src ipython :session alinbx :results output
for f in fs:
os.rename(f, f"{f}.org")
! ls | head -n 5
#+end_src
#+RESULTS:
: 00.Preface.org
: 01.UNIX-System-Overview.org
: 01.xhtml.org
: 02.UNIX-Standardization-and-Implementations.org
: 03.File-IO.org
别说python不欣赏函数式编程,是什么原因map
不工作?
解决方案
您需要使用 map 返回的迭代器。尝试
list(map(lambda x: os.rename(x, x+'.org'), fs))
推荐阅读
- reactjs - Shopify Polaris - 自定义布局问题和样式
- awk - 如何在 linux 中使用 awk 或 cat 分隔列
- ruby-on-rails - act_as_state_machine 辅助方法 rails 6
- python-imaging-library - 在简单的 Pillow 模块示例中获取“write() 参数必须是 str,而不是字节”
- c# - EF Core:如何一次插入具有新的多个相关记录的新记录
- r - R会话终止数据丢失
- java - 在每个 Switch-case 后提示用户打印,除了 case:“0 exit”
- python-3.x - 编译的 Python 函数 Executable 不是单个文件并且不运行
- r - 在 df2 中识别 df1 中的元素,然后在 df2 中使用 R 重合的那些行中添加列
- laravel - Laravel Socialite Github 提供商电子邮件为空