python 示例_帶有示例的Python File open()方法

python 示例

文件open()方法 (File open() Method)

open() method is an inbuilt method in Python, it is used to create, open or append a file.

open()方法是Python中的內置方法,用于創建,打開或附加文件。

Syntax:

句法:

    file_object = open(file_name, file_mode)

Parameter(s):

參數:

  • file_name – It is used to specify the file name.

    file_name –用于指定文件名。

  • file_mode – It is an optional parameter, it is used to specify the various file modes.

    file_mode –這是一個可選參數,用于指定各種文件模式。

    • w – Opens the file in write mode i.e. creates a file.
    • w –以寫模式打開文件,即創建文件。
    • r – Opens the file in reading mode.
    • r –以讀取模式打開文件。
    • a – Opens the file in append mode.
    • a –以追加模式打開文件。
    • x – Creates the file, if file exists it returns an error.
    • x –創建文件,如果文件存在則返回錯誤。
    • t – It is used to file modes to specify the text mode (Example: wt, rt, at, and xt).
    • t –用于文件模式以指定文本模式(例如: wt , rt , at和xt )。
    • b – It is used to file modes to specify the binary mode (Example: wb, rb, ab, and xb).
    • b –用于文件模式以指定二進制模式(例如: wb , rb , ab和xb )。

Return value:

返回值:

The return type of this method is <class '_io.TextIOWrapper'>, it returns a file object.

該方法的返回類型為<class'_io.TextIOWrapper'> ,它返回一個文件對象。

Example 1:

范例1:

# Python File open() Method with Example
print("creating files...")
# creating a file without specifying mode (b or t)
file1 = open("hello_1.txt", "w")
# creating a file in binary mode
file2 = open("hello_2.txt", "wb")
# creating a file in text mode
file3 = open("hello_3.txt", "wt")
print("file creation operation done...")
# printing the details of file objects
print(file1)
print(file2)
print(file3)

Output

輸出量

creating files...
file creation operation done...
<_io.TextIOWrapper name='hello_1.txt' mode='w' encoding='UTF-8'>
<_io.BufferedWriter name='hello_2.txt'>
<_io.TextIOWrapper name='hello_3.txt' mode='wt' encoding='UTF-8'>

Example 2:

范例2:

# Python File open() Method with Example
# creating a file
f = open("hello.txt", "w")
print("file created...")
print(f) # prints file details
# opening created file in read mode
f = open("hello.txt", "r")
print("file opened...")
print(f) # prints file details
# opening file in append mode 
f = open("hello.txt", "a")
print("file opened in append mode...")
print(f) # prints file details

Output

輸出量

file created...
<_io.TextIOWrapper name='hello.txt' mode='w' encoding='UTF-8'>
file opened...
<_io.TextIOWrapper name='hello.txt' mode='r' encoding='UTF-8'>
file opened in append mode...
<_io.TextIOWrapper name='hello.txt' mode='a' encoding='UTF-8'>

Example 3:

范例3:

# Python File open() Method with Example
# opening a file that doesn't exist
f = open("myfile.txt") # returns an error

Output

輸出量

Traceback (most recent call last):
File "main.py", line 4, in <module>
f = open("myfile.txt") # returns an error
FileNotFoundError: [Errno 2] No such file or directory: 'myfile.txt'   

翻譯自: https://www.includehelp.com/python/file-open-method-with-example.aspx

python 示例

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540985.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540985.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540985.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

php屬于腳本,php是腳本語言嗎

PHP即“超文本預處理器”&#xff0c;是一種通用開源腳本語言。PHP是在服務器端執行的腳本語言&#xff0c;與C語言類似&#xff0c;是常用的網站編程語言。PHP獨特的語法混合了C、Java、Perl以及 PHP 自創的語法。利于學習&#xff0c;使用廣泛&#xff0c;主要適用于Web開發領…

NetMarketShare:本月桌面瀏覽器市場份額幾乎沒有變化

NetMarketShare之前關于臺式機瀏覽器市場份額的報告表示&#xff0c;Google Chrome市場份額正在快速上升&#xff0c;而Edge瀏覽器市場份額正在以蝸牛的速度前進。而該公司的最新統計數據顯示&#xff0c;幾乎所有瀏覽器的市場份額或多或少保持不變。 NetMarketShare的最新統計…

treeset java_Java TreeSet add()方法與示例

treeset javaTreeSet類的add()方法 (TreeSet Class add() method) add() method is available in java.util package. add()方法在java.util包中可用。 add() method is used to add the given object(ob) to this TreeSet when it does not already exist otherwise it ignore…

php fpm www.conf,PHP7中php.ini、php-fpm和www.conf 配置

PHP7中php.ini、php-fpm和www.conf 配置php.ini是php運行核心配置文件,下面是一些常用配置extension_dir""● 設置PHP的擴展庫路徑expose_php Off● 避免PHP信息暴露在http頭中display_errors Off● 避免暴露php調用mysql的錯誤信息log_errors On● 在關閉display…

服務器電流源泉ups電源的三大形式

還記得此前12306官網癱瘓&#xff0c;回家心切急于購票的我們只能感到無比心累。雙十一前夕&#xff0c;守在購物車邊準備瘋狂購物的剁手黨們&#xff0c;遇到一直呈現加載狀態的頁面&#xff0c;不得不感嘆想要做馬云背后的那個人也要大費周折。作為一個資深網民&#xff0c;不…

timer purge_Java Timer purge()方法與示例

timer purge計時器類purge()方法 (Timer Class purge() method) purge() method is available in java.util package. purge()方法在java.util包中可用。 purge() method is used to remove all canceled tasks from the task queue of this Timer. purge()方法用于從此Timer的…

大話Linux內核中鎖機制之原子操作、自旋鎖【轉】

轉自&#xff1a;http://blog.sina.com.cn/s/blog_6d7fa49b01014q7p.html 多人會問這樣的問題&#xff0c;Linux內核中提供了各式各樣的同步鎖機制到底有何作用&#xff1f;追根到底其實是由于操作系統中存在多進程對共享資源的并發訪問&#xff0c;從而引起了進程間的競態。這…

hashmap put方法_Java HashMap put()方法與示例

hashmap put方法HashMap類的put()方法 (HashMap Class put() method) put() method is available in java.util package. put()方法在java.util包中可用。 put() method is used to link the given value element with the given key element in this HashMap. put()方法用于在…

java中jdom,java – JDOM中的命名空間(默認)

我正在嘗試使用最新的JDOM包生成XML文檔.我遇到了根元素和命名空間的問題.我需要生成這個根元素&#xff1a;xmlns"http://www.energystar.gov/manageBldgs/req"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://www.…

java enummap_Java EnumMap get()方法與示例

java enummapEnumMap類的get()方法 (EnumMap Class get() method) get() method is available in java.util package. get()方法在java.util包中可用。 get() method is used to get the value mapped with the given key element (key_ele) otherwise it returns null when no…

java后臺json傳遞,后臺json傳遞

json除了可以用于前臺傳遞&#xff0c;還可用于后臺之間傳遞。它可以傳遞List,Map,Bean等類型的數據。例如&#xff1a;User u1new User();u1.setUsername("zy");u1.setPassword("123");User u2new User();u2.setUsername("msl");u2.setPassword…

專家呼吁建安全漏洞信息共享機制并強化管控

近日&#xff0c;由中國網絡空間安全協會主辦&#xff0c;中國網絡空間安全協會網絡空間安全法律與公共政策專業委員會、北京郵電大學互聯網治理與法律研究中心、公安部第三研究所網絡安全法律研究中心、西安交通大學信息安全法律研究中心承辦的《網絡安全法&#xff08;草案二…

java的equals方法_Java Date equals()方法與示例

java的equals方法日期類equals()方法 (Date Class equals() method) equals() method is available in java.util package. equals()方法在java.util包中可用。 equals() method is used to check whether this date and the given object (o) are equals or not. equals()方法…

lnmp解析php,LNMP之 php解析

[rootLNMP ~]# vim /usr/local/nginx/conf/nginx.conf打開以下PHP 相關項且更改 scripts$fastcgi_script_name;> /usrlocal/nginx/html$fastcgi_script_name;location ~ \.php$ {root html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param…

spring歷史背景

1.2004年spring出現第一版本spring frameworl1.0 2.寫代碼永遠是最簡單的&#xff0c;后續的運維工作才是讓人感到無助的 3.spring boot在運維方面做了很多工作&#xff0c;部署&#xff0c;監控&#xff0c;度量。結合spring cloud還可以實現服務發現&#xff0c;服務降級等功…

計算機網絡中的傳輸協議是_計算機網絡中的傳輸方式

計算機網絡中的傳輸協議是傳輸方式 (Transmission Modes) The mechanism of transferring data or information between two linked devices connected over a network is referred to as Transmission Modes. 在通過網絡連接的兩個鏈接的設備之間傳輸數據或信息的機制稱為傳輸…

https 密鑰 php,https加密方式是什么

Https加密介紹Http直接通過明文在瀏覽器和服務器之間傳遞消息&#xff0c;容易被監聽抓取到通信內容。Https采用對稱加密和非對稱加密結合的方式來進行通信。Https不是應用層的新協議&#xff0c;而是Http通信接口用SSL和TLS來加強加密和認證機制。加密方式對稱加密&#xff1a…

一個前端框架應該有的一些公共函數

一、防止ie瀏覽器按backspace回退頁面 //防止后退返回頁面,如果非文本框、密碼框、文本域控件&#xff0c;或控件非可用裝填&#xff0c;則禁用后退按鍵 var uanavigator.userAgent.toLowerCase(); var isIEua.indexOf("msie")>-1; window.document.onkeydown fu…

Kruskal(P)和Prim(K)算法

最小生成樹 (Minimum Spanning Tree) An MST is a subset of the edges of the connected, undirected graph that connect all the vertices together, in which there is no forming of a cycle and there should be minimum possible total edge weight. MST是已連接的無向圖…

java get post 注解,GET/POST接收或發送數據的問題

在文章開始&#xff0c;先來回憶一下GET、POST這兩種請求方式的區別。?Http定義了與服務器交互的不同方法&#xff0c;最基本的方法有4種&#xff0c;分別是GET&#xff0c;POST&#xff0c;PUT&#xff0c;DELETE。URL全稱是資源描述符&#xff0c;我們可以這樣認為&#xff…