如何!rm python_var(在Jupyter笔记本中)

内特

我知道我可以这样做:

CSV_Files = [file1.csv, file2.csv, etc...]

%rm file1.csv
!rm file2.csv

但是我该如何做为变量。例如。

TXT_Files = [ABC.txt, XYZ.txt, etc...]

for file in TXT_Files:
  !rm file
hpaulj

rm 每次通话可以删除多个文件:

In [80]: !touch a.t1 b.t1 c.t1
In [81]: !ls *.t1
a.t1  b.t1  c.t1
In [82]: !rm -r a.t1 b.t1 c.t1
In [83]: !ls *.t1
ls: cannot access '*.t1': No such file or directory

如果起点是文件名列表:

In [116]: alist = ['a.t1', 'b.t1', 'c.t1']
In [117]: astr = ' '.join(alist)            # make a string
In [118]: !echo $astr                       # variable substitution as in BASH
a.t1 b.t1 c.t1
In [119]: !touch $astr                    # make 3 files
In [120]: ls *.t1
a.t1  b.t1  c.t1
In [121]: !rm -r $astr                    # remove them
In [122]: ls *.t1
ls: cannot access '*.t1': No such file or directory

使用Python自己的OS功能可能会更好,但是如果您对Shell的了解足够,则可以使用%magics进行很多相同的事情。


要在Python表达式中使用“魔术”,我必须使用底层函数,而不是“!”。或'%'语法,例如

import IPython
for txt in ['a.t1','b.t1','c.t1']:
    IPython.utils.process.getoutput('touch %s'%txt)

getoutput函数由%sx!!使用)(使用)使用subprocess.Popen但是,如果您进行所有工作,那么您可能还需要使用osPython本身提供功能。


文件名可能需要添加引号,以确保外壳程序不会出现语法错误:

In [129]: alist = ['"a(1).t1"', '"b(2).t1"', 'c.t1']
In [130]: astr = ' '.join(alist)
In [131]: !touch $astr
In [132]: !ls *.t1
'a(1).t1'   a.t1  'b(2).t1'   b.t1   c.t1

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在我的jupyter笔记本中添加python3内核?

如何在Jupyter笔记本中运行Python异步代码?

如何在python中获取当前的jupyter笔记本服务器?

如何从离线的可打印python jupyter笔记本中的选定点获取数据?

如何在PyCharm中将Markdown嵌入用作jupyter笔记本的python文件中

一旦满足条件,如何停止python程序(在jupyter笔记本中)

对于分类数据,如何在表而不是列表中显示结果?(Jupyter笔记本/ Python)

如何在jupyter笔记本中隐藏单元格输出(VSCode + Python扩展)?

如何每天自动运行python jupyter笔记本

如何从Jupyter笔记本中获取原始代码?

如何检查您是否在Jupyter笔记本中

如何在Jupyter笔记本中嵌入gif?

如何在Jupyter笔记本中包装代码/文本

如何在Jupyter笔记本中配置缩进大小?

如何在Jupyter笔记本中对熊猫使用tqdm?

如何在jupyter笔记本中安装sympy

如何从笔记本中查找Jupyter Notebook版本

如何从Jupyter笔记本中删除emacs键绑定?

如何在Pycharm笔记本中停止Jupyter Server

如何从GitHub保存Jupyter笔记本

如何导出整个Jupyter笔记本?

如何从终端启动jupyter笔记本

如何将jupyter笔记本目录中的模块导入较低目录中的笔记本?

如何将其他 jupyter 笔记本中的情节图集成到新笔记本中?

如何根据python中pandas数据框中的列进行分组并按降序排列?(Jupyter笔记本)

VSCODE JUPYTER - 如何在笔记本 (.ipynb) 中显示与 VSCode 中的 Python (.py) 相同的自动完成?

如何在Jupyter笔记本中的相同位置更新python循环中的matpplotlib lib图?

从Ubuntu WSL运行Jupyter时,如何访问Windows中存储的Jupyter笔记本?

如何在 Google Colab 中的笔记本中显示图像(如在 Anacondan Jupyter Notebook 中)?