笨辦法學python 3 48題_附錄練習 8-10 笨辦法學Python3

 2023-11-30 阅读 22 评论 0

摘要:附錄練習 8 來回移動 (pushd, popd)在這個練習中,你將學習如何用 pushd 命令保存你當前的位置然后去到一個新的位置,以及如何用 popd 命令返回之前保存的位置。55.9.1 跟我做Linux/macOS練習 8 會話$ cd temp$ mkdir i/like/icecream$ pushd i/like/icecream~/t

附錄練習 8 來回移動 (pushd, popd)

在這個練習中,你將學習如何用 pushd 命令保存你當前的位置然后去到一個新的位置,以及如何用 popd 命令返回之前保存的位置。

55.9.1 跟我做

Linux/macOS

練習 8 會話

$ cd temp

$ mkdir i/like/icecream

$ pushd i/like/icecream

~/temp/i/like/icecream ~/temp

$ popd

~/temp

$ pwd

~/temp

$ pushd i/like

~/temp/i/like ~/temp

$ pwd

~/temp/i/like

$ pushd icecream

~/temp/i/like/icecream ~/temp/i/like ~/temp

$ pwd

~/temp/i/like/icecream

$ popd

~/temp/i/like ~/temp

$ pwd

~/temp/i/like

$ popd

~/temp

$ pushd i/like/icecream

~/temp/i/like/icecream ~/temp

$ pushd

~/temp ~/temp/i/like/icecream

$ pwd

~/temp

$ pushd

~/temp/i/like/icecream ~/temp

$ pwd

~/temp/i/like/icecream

$

Windows

練習 8 Windows 會話

> cd temp

> mkdir i/like/icecream

Directory: C:\Users\zed\temp\i\like

Mode LastWriteTime Length Name

---- ------------- ------ ----

d---- 12/20/2011 11:05 AM icecream

> pushd i/like/icecream

> popd

> pwd

Path

----

C:\Users\zed\temp

> pushd i/like

> pwd

Path

----

C:\Users\zed\temp\i\like

> pushd icecream

> pwd

Path

----

C:\Users\zed\temp\i\like\icecream

> popd

> pwd

Path

----

C:\Users\zed\temp\i\like

> popd

>

警告!

在 Windows 系統下,你一般不用像 Linux 系統那樣用 -p ,但是我想這應該是最近的更新,如果你用老的 Windows 系統的 Powershell,應該還是需要 -p 的,所以每個人的情況可能不太一樣,你可以試試看。

55.9.2 你學到的

你正在通過這些命令進入程序員的世界,這些命令很常用,所以我必須要教給你們。它們能讓你暫時地去到別的目錄,然后再回來,并在兩者之間隨意切換。

pushd 命令會把你當前的目錄“push”到一個列表里,然后它會切換到另外一個目錄,就好像在說:“保存我現在的位置,然后去到那兒”。

popd 命令則是把你從你之前去到的目錄那里拉回來。

最后,在 Unix 下使用 pushd 的話,如果你后面不加任何東西,它會在你的當前目錄和你之前保存的目錄之間來回切換。但是在 Powershell 下這個就不適用了。

55.9.3 附加練習

用這些命令在你電腦的目錄之間來回移動。

移除 i/like/icecream 目錄,然后自己創建一些,并在它們中間來回切換。

跟自己解釋 pushd 和 popd 的輸入結果,注意它和堆棧的概念很類似。

雖然你已經知道了,但是要記住 mkdir -p (在 Linux/MacOS 下)會創建一個完整的路徑,即使所有的目錄都不存在。這也就是我在本練習開頭所做的。

記住 Window 也會創建一個完整的路徑,并且不需要 -p 。

附錄練習 9 創建空文件 (Touch, New-Item)

在這個練習中你將學習如何使用 touch (MacOS)或者 new-item (Windows) 命令來創建空文件。

55.10.1 跟我做

Linux/macOS

練習 9 會話

$ cd temp

$ touch iamcool.txt

$ ls iamcool.txt

$

Windows

練習 9 Windows 會話

> cd temp

> New-Item iamcool.txt -type file

> ls

Directory: C:\Users\zed\temp

Mode LastWriteTime Length Name

---- ------------- ------ -----

a--- 12/17/2011 9:03 AM iamcool.txt

>

55.10.2 你學到的

你學習了如何創建空文件。在 Unix 系統下用 touch ,在 Windows 系統下用 New-Item 。

55.10.3 附加練習

Unix:創建一個目錄,切換到該目錄下,然后在它里面創建一個文件,然后再切換到該目錄的上一次,用 rmdir 命令移除該目錄。你會收到報錯,試著理解一下為什么。

Windows:做同樣的事情,但是你不會收到報錯,你會收到一個提示符問你是否真的要移除這個目錄。

附錄練習 10 復制文件 (cp)

在這個練習中,你將學習如何用 cp 命令把一個文件從一個地址復制到另一個地址。

55.11.1 跟我做

Linux/macOS

練習 10 會話

$ cd temp

$ cp iamcool.txt neat.txt

$ ls

iamcool.txt neat.txt

$ cp neat.txt awesome.txt

$ ls

awesome.txt iamcool.txt neat.txt

$ cp awesome.txt thefourthfile.txt

$ ls

awesome.txt iamcool.txt neat.txt thefourthfile.txt

$ mkdir something

$ cp awesome.txt something/

$ ls

awesome.txt iamcool.txt neat.txt something thefourthfile.txt

$ ls something/ awesome.txt

$ cp -r something newplace

$ ls newplace/ awesome.txt

$

Windows

練習 10 Windows 會話

> cd temp

> cp iamcool.txt neat.txt

> ls

Directory: C:\Users\zed\temp

Mode LastWriteTime Length Name

---- ------------- ------ ----

-a--- 12/22/2011 4:49 PM 0 iamcool.txt

-a--- 12/22/2011 4:49 PM 0 neat.txt

> cp neat.txt awesome.txt

> ls

Directory: C:\Users\zed\temp

Mode LastWriteTime Length Name

---- ------------- ------ ----

-a--- 12/22/2011 4:49 PM 0 awesome.txt

-a--- 12/22/2011 4:49 PM 0 iamcool.txt

-a--- 12/22/2011 4:49 PM 0 neat.txt

> cp awesome.txt thefourthfile.txt

> ls

Directory: C:\Users\zed\temp

Mode LastWriteTime Length Name

---- ------------- ------ ----

-a--- 12/22/2011 4:49 PM 0 awesome.txt

-a--- 12/22/2011 4:49 PM 0 iamcool.txt

-a--- 12/22/2011 4:49 PM 0 neat.txt

-a--- 12/22/2011 4:49 PM 0 thefourthfile.txt

> mkdir something

Directory: C:\Users\zed\temp

Mode LastWriteTime Length Name

---- ------------- ------ ----

d---- 12/22/2011 4:52 PM something

> cp awesome.txt something/

> ls

Directory: C:\Users\zed\temp

Mode LastWriteTime Length Name

---- ------------- ------ ----

d---- 12/22/2011 4:52 PM something

-a--- 12/22/2011 4:49 PM 0 awesome.txt

-a--- 12/22/2011 4:49 PM 0 iamcool.txt

-a--- 12/22/2011 4:49 PM 0 neat.txt

-a--- 12/22/2011 4:49 PM 0 thefourthfile.txt

> ls something

Directory: C:\Users\zed\temp\something

Mode LastWriteTime Length Name

---- ------------- ------ ----

-a--- 12/22/2011 4:49 PM 0 awesome.txt

> cp -recurse something newplace

> ls newplace

Directory: C:\Users\zed\temp\newplace

Mode LastWriteTime Length Name

---- ------------- ------ ----

-a--- 12/22/2011 4:49 PM 0 awesome.txt

>

55.11.2 你學到的

現在你會復制文件了,它很簡單。在這個練習中,我還創建了一個新目錄,并且把一個文件復制到了那個新目錄中。

我要告訴你一個關于程序員和系統管理員的秘密。他們很懶,我也很懶,我的朋友同樣很懶。這也正是為什么我們要用計算機。我們熱衷于讓計算機為我們做無聊的事情。這個練習到目前為止你已經輸入了很多重復的命令來學習它們,但是實際情況不是這樣的,通常如果你發現自己在做一些無聊和重復的事情,就已經有一個程序員在想辦法如何讓這件事情變得簡單,你只是不知道而已。

關于程序員的另一件事情就是,他們可能沒你想的那么聰明。如果你覺得他們輸入的東西有多么高深莫測,那你就錯了。在你做練習的時候,你可以先試著先想想這些命令的名字和含義,然后再輸入。一般你會想到一個名字或者一些縮寫。如果你還是想象不出來,就回過頭復習一下或者在網上搜一搜。

5.11.3 附加練習

用 cp -r 命令復制更多包含文件的目錄。

把一個文件復制到你的 home 目錄或者桌面。

在圖形用戶界面找到這些文件,然后用文本編輯器打開它們。

注意我有時候會放一個 / 在目錄結尾,這樣是為了確保這是一個目錄,如果不是的話,我會收到報錯。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/186362.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息