《Learn python3 the hard way》ex15閱讀檔案
今天學習的是argv的filename屬性以及檔案的open和read,txt是在相同路徑內已存在的一個txt
from sys import argv
scrip, filename = argv
txt = open(filename) #開啟路徑並賦值給txt
print(f"Here's your file {filename}:")
print(txt.read())#txt的read屬性,讀txt的內容
print("Type the filename again:")
file_again = input(">")#利用input將路徑名賦值給file_again
txt_again = open(file_again)#開啟路徑
print(txt_again.read())
相關推薦
《Learn python3 the hard way》ex15閱讀檔案
今天學習的是argv的filename屬性以及檔案的open和read,txt是在相同路徑內已存在的一個txt from sys import argv scrip, filename = argv txt = open(filename) #開啟路徑並賦值給txt print(
《Learn python3 the hard way》ex16 讀寫檔案
作者想讓我們記住的命令: close- 關閉檔案,就像編輯器中的“檔案->另存為”一樣 read- 讀取檔案內容。你可以把讀取結果賦值給一個變數 readline- 只讀取文字檔案的一行內容 truncate- 清空檔案。清空的時候要當心 write("stuff")-給檔案寫入一些東
《Learn python3 the hard way》ex20函式和檔案
1…seek(0)用於重置指標到檔案開頭的位置,重新開始 因為在列印完全文時游標已至文尾,要重新列印一行一行需要游標回到開頭。 2…readline()是按行來讀檔案,所以每次列印時只打印一行。 from sys import argv script,inpu
《Learn python3 the hard way》ex17
今天新學了一個os.path 這個包裡exists這個功能,驗證是否存在,存在則顯示TRUE不存在則顯示False。 from sys import argv from os.path import exists script, from_file, to_file = argv p
《Learn python3 the hard way》ex14 Prompting and Passing
今天是使用提示語>以及運用argv 和 input進行互動。 from sys import argv script, user_name = argv prompt = ">" print(f"Hi {user_name},I'm the {script} script
《Learn python3 the hard way》ex13 Parameters,Unpacking,Variables
from sys import argv # read the WYSS section for how to run this: script, first, second, third = argv print("The script is called:", script) print
《Learn python3 the hard way》ex12 Prompting People
Prompting People 今天在敲這個程式碼的時候,我還在想我昨天是不是做了今天的份了,怎麼那麼像的,回頭一看還是有 區別的, 昨天是先提前將提示語打印出,末位需要用end=""連線才能不換行,再用input給變數賦值。 今天是直接在inp
《Learn python3 the hard way》ex11 Asking Quesions
之前都是直接輸入列印結果的,這節課學習的是如何在程式碼執行時,由使用物件自行輸入,打印出結果,這就是一個簡單的互動。 方法input(),生成的是字串,如果需要使結果是數字用int(input()) print("How old are you?", end="") age = inpu
《Learn python3 the hard way》ex10 What was that
\在python裡面表示轉義符,功能: 把python中的一些特殊字元有功能的字元打印出來,比如\可以打印出\ 可以擁有一些功能,比如換行\n,比如縮排\t tabby_cat = "\tI'm tabbed in." persian_cat
《Learn python3 the hard way》ex9
Printing,Printing,Printing 今天學習的主要是如何打換行的文字 法一:換行符\n 法二 """""" 一對三引號,三個引號中間無空格 # Here's some new strange stuff, remember type it exactly. day
Learn Python3 the hard way 第一天總結 命令列(1)
附錄-命令列快速入門(1) command line interface 簡稱 CLI ,可以在mac OS 上通過一些輸入進行一些操作。 1如何在迷路後怎樣回家 命令: pwd:列印工作目錄cd 更改目錄 上機操作: 在 CLI 中輸入:pwdcd ~其中,pwd 列印當前的目錄,cd
06《learn Python3 the hard way》一起來敲程式碼,每天進步一點點
lesson 6 字串和文字 從今天開始打算每天學習《learn python3 the hard way》,並在部落格裡記錄筆記,希望能堅持下來,總共有52課,我記得,從第六課開始,以下是我今天學習到的: 原始碼: types_of_people = 10 x
Learn Python3 the hard way 第二天總結 命令列(2)
複製檔案 命令:cp含義:很簡單,就是把一個檔案複製成一個新檔案而已。使用 cp -r命令可以複製一些包含檔案的目錄 移動檔案 命令:mv含義:對檔案進行"rename". 檢視檔案內容 命令:less含義:這是檢視檔案內容的一種方法,它有用的地方在於,如果檔案內容有很多行,它會將其分頁,
《Learn python3 the hard way》ex19 函式和變數
def cheese_and_crackers(cheese_count, boxes_of_crackers): # 定義一個有兩個引數的函式 print(f"You have {cheese_count} cheeses!") print(f
Learn Python the Hard Way
world target python 2 set works vid names article con Reference Zed Shaw - Learn Python the Hard Way Ex0: The Setup Windows Python 2, A
learn python the hard way 習題1~5總結
習題1 print 語句print('Yay! Printing.')print('I "said" do not touch this') 習題2:註釋和 # 號 #(octothorpe),用於註釋一條語句,在 # 之後的內容全部被忽略。 習題3:數字和數字計算 當進行關係運算的時候,比如 3+
learn python the hard way 習題6~10總結
習題6總結 定義字串: 名字 = 值 其他 你也可以用 {types_of_people}的方式把它放在任何字串中。 也就是說你可以在其他字串中新增{},然後前面加一個 f,可用print()進行直接列印。 f-string 特殊的字串型別:f-string舉例:f" some stu
learn python the hard way 習題18~25總結
定義函式和呼叫函式的語法 定義函式 形式: def functionName(p1,p2): statement other statement 需要注意: 緊跟者函式定義的程式碼是否使用了4個空格的縮排?不能多,也不能少 函式結束的位置是否取消了縮排?
learn python the hard way習題31~40總結以及列表的擴充套件知識
Python 中的列表: 形式:[ 表示開啟一個列表,中間的專案用 , 隔開,然後列表以 ] 結束。 for迴圈 兩種形式: for i in ArrayName: for i in range(0,6): 其中的變數 i 在使用 for 迴圈的時候, Python 自動建立,作為進行
【Python】Learn Python the hard way, ex47 自動化測試,環境變數設定
class Room(object): def __init__(self, name, description): self.name = name self.description = description