Làm cách nào để lưu đầu ra shell vào một tệp trong python?

Khi làm việc tương tác với trình thông dịch Python tiêu chuẩn, một trong những điều khó chịu là cần phải chuyển đổi giữa nhiều cửa sổ để truy cập các công cụ Python và công cụ dòng lệnh hệ thống. IPython thu hẹp khoảng cách này và cung cấp cho bạn cú pháp để thực thi các lệnh trình bao trực tiếp từ bên trong thiết bị đầu cuối IPython. Điều kỳ diệu xảy ra với dấu chấm than. bất kỳ thứ gì xuất hiện sau

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
5 trên một dòng sẽ không được thực thi bởi nhân Python, mà bởi dòng lệnh hệ thống

Điều sau đây giả định rằng bạn đang sử dụng hệ thống giống Unix, chẳng hạn như Linux hoặc Mac OSX. Một số ví dụ sau sẽ không thành công trên Windows, mặc định sử dụng một loại trình bao khác [mặc dù với thông báo năm 2016 về trình bao Bash gốc trên Windows, điều này sẽ sớm không còn là vấn đề nữa. ]. Nếu bạn không quen với các lệnh shell, tôi khuyên bạn nên xem lại Hướng dẫn Shell được tổng hợp bởi Software Carpentry Foundation luôn xuất sắc

Giới thiệu nhanh về Shell¶

Phần giới thiệu đầy đủ về cách sử dụng shell/terminal/command-line nằm ngoài phạm vi của chương này, nhưng đối với những người chưa quen, chúng tôi sẽ giới thiệu nhanh tại đây. Shell là một cách để tương tác bằng văn bản với máy tính của bạn. Kể từ giữa những năm 1980, khi Microsoft và Apple giới thiệu phiên bản đầu tiên của hệ điều hành đồ họa phổ biến hiện nay của họ, hầu hết người dùng máy tính đã tương tác với hệ điều hành của họ thông qua cách nhấp vào menu và chuyển động kéo và thả quen thuộc. Nhưng các hệ điều hành đã tồn tại từ lâu trước các giao diện người dùng đồ họa này và chủ yếu được điều khiển thông qua các chuỗi nhập văn bản. tại dấu nhắc, người dùng sẽ nhập lệnh và máy tính sẽ thực hiện những gì người dùng yêu cầu. Những hệ thống nhắc ban đầu đó là tiền thân của vỏ và thiết bị đầu cuối mà hầu hết các nhà khoa học dữ liệu tích cực vẫn sử dụng ngày nay

Một người nào đó không quen thuộc với trình bao có thể hỏi tại sao bạn lại bận tâm đến điều này, khi có thể đạt được nhiều kết quả chỉ bằng cách nhấp vào biểu tượng và menu. Người dùng shell có thể trả lời bằng một câu hỏi khác. tại sao phải săn các biểu tượng và nhấp vào menu khi bạn có thể hoàn thành mọi việc dễ dàng hơn nhiều bằng cách nhập?

Ví dụ: đây là mẫu của phiên hệ vỏ Linux/OSX nơi người dùng khám phá, tạo và sửa đổi các thư mục và tệp trên hệ thống của họ [

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
0 là dấu nhắc và mọi thứ sau ký hiệu
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
1 là lệnh đã nhập; văn bản đó là

osx:~ $ echo "hello world"             # echo is like Python's print function
hello world

osx:~ $ pwd                            # pwd = print working directory
/home/jake                             # this is the "path" that we're sitting in

osx:~ $ ls                             # ls = list working directory contents
notebooks  projects 

osx:~ $ cd projects/                   # cd = change directory

osx:projects $ pwd
/home/jake/projects

osx:projects $ ls
datasci_book   mpld3   myproject.txt

osx:projects $ mkdir myproject          # mkdir = make new directory

osx:projects $ cd myproject/

osx:myproject $ mv ../myproject.txt ./  # mv = move file. Here we're moving the
                                        # file myproject.txt from one directory
                                        # up [../] to the current directory [./]
osx:myproject $ ls
myproject.txt

Lưu ý rằng tất cả những điều này chỉ là một cách nhỏ gọn để thực hiện các thao tác quen thuộc [điều hướng cấu trúc thư mục, tạo thư mục, di chuyển tệp, v.v. ] bằng cách nhập lệnh thay vì nhấp vào biểu tượng và menu. Lưu ý rằng chỉ với một vài lệnh [

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
3,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
4,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
5,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
6 và
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
7], bạn có thể thực hiện nhiều thao tác tệp phổ biến nhất. Đó là khi bạn vượt ra ngoài những điều cơ bản này, cách tiếp cận trình bao trở nên thực sự mạnh mẽ

Lệnh Shell trong IPython¶

Bất kỳ lệnh nào hoạt động ở dòng lệnh đều có thể được sử dụng trong IPython bằng cách đặt trước nó bằng ký tự

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
5. Ví dụ: các lệnh
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
4,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
3 và
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
11 có thể được chạy như sau

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell

Truyền các giá trị đến và từ Shell¶

Các lệnh Shell không chỉ có thể được gọi từ IPython mà còn có thể được thực hiện để tương tác với không gian tên IPython. Ví dụ: bạn có thể lưu đầu ra của bất kỳ lệnh shell nào vào danh sách Python bằng toán tử gán

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
1

Lưu ý rằng những kết quả này không được trả về dưới dạng danh sách mà dưới dạng loại trả về trình bao đặc biệt được xác định trong IPython

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
4

Điều này trông và hoạt động rất giống một danh sách Python, nhưng có chức năng bổ sung, chẳng hạn như các phương thức

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
12 và
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
13 và các thuộc tính
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
14,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
15 và
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
16 cho phép bạn tìm kiếm, lọc và hiển thị kết quả theo những cách thuận tiện. Để biết thêm thông tin về những điều này, bạn có thể sử dụng các tính năng trợ giúp tích hợp của IPython

Giao tiếp theo hướng khác–chuyển các biến Python vào shell–có thể sử dụng cú pháp

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
17

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
1

Dấu ngoặc nhọn chứa tên biến, được thay thế bằng nội dung của biến trong lệnh shell

Lệnh ma thuật liên quan đến Shell¶

Nếu chơi với các lệnh shell của IPython một lúc, bạn có thể nhận thấy rằng bạn không thể sử dụng

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
18 để điều hướng hệ thống tệp

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
3

Lý do là các lệnh shell trong sổ ghi chép được thực thi trong một subshell tạm thời. Nếu bạn muốn thay đổi thư mục làm việc theo cách lâu dài hơn, bạn có thể sử dụng lệnh ma thuật

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
19

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
5

Trên thực tế, theo mặc định, bạn thậm chí có thể sử dụng cái này mà không cần ký hiệu

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
40

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
7

Đây được gọi là hàm

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
41 và hành vi này có thể được chuyển đổi bằng hàm ma thuật
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
42

Bên cạnh

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
19, các chức năng ma thuật giống như lớp vỏ có sẵn khác là
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
44,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
45,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
46,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
47,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
48,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
49,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
10,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
11,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
12,
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
13 và
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
14, bất kỳ chức năng nào trong số đó có thể được sử dụng mà không cần ký hiệu
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
40 nếu
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
41 được bật. Điều này giúp bạn gần như có thể coi dấu nhắc IPython như thể đó là trình bao bình thường

In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
4

Quyền truy cập vào trình bao này từ trong cùng một cửa sổ đầu cuối với phiên Python của bạn có nghĩa là ít phải chuyển đổi qua lại giữa trình thông dịch và trình bao khi bạn viết mã Python của mình

Chủ Đề