最近有點忘記了一些常見命令,這里就記錄一下,懶得找了。
文章目錄
- 一、文件操作命令
- 1. `%cd` 工作目錄
- 2. `%pwd` 顯示路徑
- 3. `!ls` 列出文件
- 4. `!cp` 復制文件
- 5. `!mv` 移動或重命名
- 6. `!rm` 刪除
- 二、代碼調試
- 1. `%time` 時間
- 2. `%timeit` 平均時長
- 3. `%debug` 調試
- 4. `%run` 跑python
- 5. `%load` 加載
- 三、魔術命令
- 1. `%matplotlib` 顯示圖片
- 2. `%pylab` 導入Numpy和Matpolotlib
- 3. `%%writefile` 單元格內容寫入文件
- 4. `%history` 歷史命令
- 5. `%who` 列出變量名
- 6. `%whos` 列出變量名及詳情
- 7. `%xmode` 異常顯示
- 四、帶嘆號的
- 1. `!pip`
- 2. `!conda`
- 3. `!git`
- 4. `!wget`
- 5. `!curl`
- 6. `!mkdir` 創建目錄
一、文件操作命令
1. %cd
工作目錄
用于切換當前工作目錄。
%cd /home/test/test1/src
或者用python的代碼
import os
os.chdir('/home/test/test1/src') # 切換
print(os.path.abspath('.')) # 打印看看
2. %pwd
顯示路徑
顯示當前工作目錄的路徑。例如:
%pwd
輸出:
'/home/user/works'
3. !ls
列出文件
列出當前目錄下的文件和文件夾。例如:
!ls
4. !cp
復制文件
復制文件。例如:
!cp source_file.txt destination_file.txt
5. !mv
移動或重命名
移動或重命名文件。例如:
!mv old_name.txt new_name.txt
6. !rm
刪除
刪除文件。例如:
!rm file_to_delete.txt
二、代碼調試
1. %time
時間
用于測量單個代碼塊的執行時間。例如:
%time sum(range(1000000))
輸出:
CPU times: user 0.02 s, sys: 0.00 s, total: 0.02 s
Wall time: 0.02 s
2. %timeit
平均時長
多次運行代碼塊,給出平均執行時間,適合比較不同實現的性能。例如:
%timeit sum(range(1000000))
輸出:
10 loops, best of 3: 100 ms per loop
3. %debug
調試
進入調試模式,用于調試代碼中的錯誤。例如:
%debug
4. %run
跑python
運行一個 Python 腳本文件。例如:
%run test.py
!python test.py
5. %load
加載
加載一個文件的內容到當前單元格。例如:
%load data_processing.py
三、魔術命令
1. %matplotlib
顯示圖片
用于在 Notebook 中顯示 Matplotlib 圖形。例如:
%matplotlib inline
2. %pylab
導入Numpy和Matpolotlib
一次性導入 NumPy 和 Matplotlib,并使其可用。例如:
%pylab inline
3. %%writefile
單元格內容寫入文件
將單元格內容寫入文件。例如:
%%writefile hello_world.py
print("Hello, World!")
4. %history
歷史命令
顯示歷史命令記錄。例如:
%history
5. %who
列出變量名
列出當前命名空間中的變量名。例如:
%who
6. %whos
列出變量名及詳情
列出當前命名空間中的變量名及其詳細信息。例如:
%whos
7. %xmode
異常顯示
設置異常顯示模式。例如:
%xmode Verbose
四、帶嘆號的
1. !pip
在 Notebook 中安裝 Python 包。例如:
!pip install numpy
!pip install -r requirements.txt --user
!pip install -r xxx/requirements.txt -t xxx/external-libraries
- -r requirements.txt:表示從 requirements.txt 文件中讀取依賴列表。
- -t xxx/external-libraries:表示將安裝的庫放到指定的目錄 xxx/external-libraries 下。
2. !conda
在 Notebook 中使用 Conda 命令。例如:
!conda list
3. !git
在 Notebook 中使用 Git 命令。例如:
!git clone https://github.com/user/repo.git
4. !wget
下載文件。例如:
!wget https://example.com/data.csv
5. !curl
發送 HTTP 請求。例如:
!curl https://api.example.com/data
6. !mkdir
創建目錄
!mkdir -p xxx/external-libraries