Besides %cd, other available shell-like magic functions are %cat, %cp, %env, %ls, %man, %mkdir, %more, %mv, %pwd, %rm, and %rmdir
In [11]: !pwd /home/jake/projects/myproject
In [12]: !cd ..
In [13]: !pwd /home/jake/projects/myproject
In [16]: mkdir tmp
In [17]: ls myproject.txt tmp/
In [18]: cp myproject.txt tmp/
In [19]: ls tmp myproject.txt
In [20]: rm -r tmp
Store variables to a file
1 2 3 4 5 6 7 8 9 10 11 12 13
# https://ipython.readthedocs.io/en/stable/config/extensions/storemagic.html # %store magic for lightweight persistence. 用途:方便快捷存储和恢复指定变量,也可以讲指定变量内容写入文件。
%store bar # Store the current value of the variable bar to disk 保存在 autorestore 目录。 %store -r bar # Refresh specified variables and aliases from store %store -d bar # Remove the variable and its value from storage %store foo >a.txt # Store value of foo to new file a.txt %store foo >>a.txt # Append value of foo to file a.txt
In [1]: a = 100 In [2]: b = 200 In [3]: c = a + b In [4]: c Out[4]: 300
In [5]: %save filename.py 1-4
List all the variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
In [1]: a = 100 In [2]: name = "Sebastian" In [3]: squares = [x*x for x inrange(100)] In [4]: squares_sum = sum(squares) In [5]: defsay_hello(): ...: print("Hello!")
In [6]: %whos Variable Type Data/Info ----------------------------------- a int100 name str Sebastian say_hello function <function say_hello at 0x111b60a60> squares list n=100 squares_sum int328350
IPython scripts
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ ls file1.py file2.py file3.py file4.py wishes.ipy
$ cat wishes.ipy files = !ls # Run all the files with .py suffix for file in files: if file.endswith(".py"): %run $file
# 写入部署脚本 text = """#!/bin/sh set -xe mkdir -p ~/deploy/golang/ && cd ~/deploy/golang/ # Linux golang 1.19 install from https://docs.studygolang.com/doc/install wget -q https://studygolang.com/dl/golang/go1.20.6.linux-amd64.tar.gz rm -rf ~/deploy/golang/go && tar -C ~/deploy/golang/ -xzf go1.20.6.linux-amd64.tar.gz # echo 'export PATH=$PATH:/usr/local/go/bin' >> $HOME/.profile export PATH=$PATH:~/deploy/golang/go/bin go version go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn # subfinder requires go1.19 to install successfully. Run the following command to install the latest version: go install github.com/projectdiscovery/subfinder/v2/cmd/subfinder@latest # httpx requires go1.19 to install successfully. Run the following command to get the repo: go install github.com/projectdiscovery/httpx/cmd/httpx@latest """ %store text >/tmp/deploy_env.sh print("Write script done.")
# 执行部署脚本 ! sh /tmp/deploy_env.sh print("Install golang done.")