robert1003
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- slideOptions: transition: slide --- # File I/O ## Sprout Py2021 @robert1003 ---- I/O = Input / Output = 輸入 / 輸出 <span>File I/O = 檔案輸入 / 輸出<!-- .element: class="fragment" data-fragment-index="2" --></span> ---- 檔案輸入輸出發生在什麼時候? ---- 輸入(Input):開啟檔案的時候 ![](https://i.imgur.com/8s2Fwcg.jpg) ---- 輸出(Output):儲存檔案的時候 ![](https://i.imgur.com/VlB9pil.jpg) --- ## Outline <!--- + 一些準備 ---> + 目錄 + 檔案 + 路徑 + 開檔 + 讀檔 + 寫檔 + 關檔 + 常用寫法 以下是一些補充 + pickle + JSON --- # 目錄(Directory) ---- 其實就是資料夾 <span>桌面也是一個目錄(資料夾)<!-- .element: class="fragment" data-fragment-index="1" --></span> --- # 檔案(File) ---- 這些都是檔案: + txt + zip + docx + py + cpp + ... ---- 檔案可以分成兩種: | 文字檔(text file)| 二進位檔(binary file)| |-|-| |你看得懂|你看不懂| | html, txt | png, zip, docx | | ![](https://i.imgur.com/HBqN8Wh.png) | ![](https://i.imgur.com/Rm39H8Z.png) | --- # 路徑(Path) ---- 其實就是檔案的位置 ---- 路徑也可以分成兩種 <span>絕對路徑&相對路徑<!-- .element: class="fragment" data-fragment-index="2" --></span> ---- ## 絕對路徑 可以看成地址(? ---- Windows: `C:\windows\a.dll` <span>C槽底下的windows資料夾裡的a.dll<!-- .element: class="fragment" data-fragment-index="3" --></span> ---- Linux: `/tmp/b07902047/out.png` <span>根目錄底下的tmp底下的b07902047的out.png<!-- .element: class="fragment" data-fragment-index="4" --></span> ---- ## 相對路徑 直接舉例可能比較好懂 ---- `hello2.py` <span>當前目錄下的hello2.py<!-- .element: class="fragment" data-fragment-index="3" --></span> ---- `fileIO/hello3.py` <span>當前目錄下的fileIO資料夾裡面的hello3.py<!-- .element: class="fragment" data-fragment-index="3" --></span> ---- `../hello.py` <span>上一層資料夾的hello.py<!-- .element: class="fragment" data-fragment-index="3" --></span> ---- `../robert/ans/hw1.pdf` <span>上一層資料夾的robert資料夾裡面的ans資料夾裡面的hw1.pdf<!-- .element: class="fragment" data-fragment-index="3" --></span> ---- 所以他們差在哪裡? <span> 絕對:一定從最上面的目錄開始描述 相對:從當前目錄開始描述<!-- .element: class="fragment" data-fragment-index="3" --></span> --- # 開檔 ---- 一個指令 ```python [file_object] = open([file_path], [mode]) ``` ```python f = open('hello.py', 'r') ``` <span>file_object: 檔案物件 file_path: 檔案路徑 mode: 開檔模式 <!-- .element: class="fragment" data-fragment-index="4" --></span> ---- 檔案物件(file_object)? <span>可以自己取名字<!-- .element: class="fragment" data-fragment-index="0" --></span> <span>讓python可以存取電腦上的檔案<!-- .element: class="fragment" data-fragment-index="1" --></span> ---- 檔案路徑(file_path)? <span>就是剛剛講的路徑 絕對路徑跟相對路徑都可以<!-- .element: class="fragment" data-fragment-index="0" --></span> ---- 開檔模式(mode)? <span>簡單來說就是跟python講你現在要拿這個檔案幹嘛<!-- .element: class="fragment" data-fragment-index="0" --></span> <span> | 模式 | 功能 | |-|-|-| | r | 讀 | | w | 寫 | | a | 寫在最後面 | <!-- .element: class="fragment" data-fragment-index="0" --></span> <span>w: 如果檔案不存在會建一個新的,存在的話會覆蓋原本的檔案<!-- .element: class="fragment" data-fragment-index="0" --></span> --- # 讀檔 ---- 兩個指令 ---- 讀一整列 ```python [string] = [file_object].readline() ``` ```python f = open('hello.txt', 'r') s1 = f.readline() s2 = f.readline() print(s1, end='') print(s2, end='') ``` <span>string: 把回傳的字串存到string裡 file_object: 檔案物件 <!-- .element: class="fragment" data-fragment-index="4" --></span> ---- 讀特定數量的字元 ```python [string] = [file_object].read([length]) ``` ```python f = open('hello.txt', 'r') s = f.read(11) print(s, end='') ``` <span>string: 把回傳的字串存到string裡 file_object: 檔案物件 length: 要讀幾個字元,型別是int <!-- .element: class="fragment" data-fragment-index="4" --></span> ---- 如果不打length會發生什麼事? ```python s = f.read() ``` <span>他會一次把整個檔案讀進來<!-- .element: class="fragment" data-fragment-index="3" --></span> --- # 寫檔 ---- 一個指令 ```python [file_object].write([string]) ``` ```python f = open('hola.txt', 'w') f.write('Hola, mondo!\n') f.write('Me llamo Robert.\n') ``` <span>string: 要寫入的字串 file_object: 檔案物件 <!-- .element: class="fragment" data-fragment-index="4" --></span> --- # 關檔 ---- 一個指令 ```python [file_object].close() ``` ```python f.close() ``` <span>file_object: 檔案物件 <!-- .element: class="fragment" data-fragment-index="4" --></span> ---- 什麼時候要關檔? 使用完的時候(廢話),暫時不需要他了 ---- 一定要關檔案嗎? ---- 不關檔案的壞處 1. 佔你記憶體的空間,讓你程式變慢 2. 在你關檔之前,檔案都不會被真的寫入 3. 你有可能開超過數量上限的檔案 4. 看起來很不專業 ---- 關於2.如果你想馬上寫入檔案怎麼辦? <span> ```python f = open('hola.txt', 'r') print(f.read(), end='') f.flush() ``` <!-- .element: class="fragment" data-fragment-index="4" --></span> --- # 常用寫法 ---- 常用寫法 1 ```python with open([file_name], [mode]) as [file_object]: # do something ``` ```python with open('hola.txt', 'r') as f: print(f.read(), end='') ``` 只有在那個迴圈裡檔案才是開的(所以不用關檔) <span>注意上面的寫法等於下面的寫法:<!-- .element: class="fragment" data-fragment-index="4" --></span> <span> ```python f = open('hola.txt', 'r') print(f.read(), end='') f.close() ``` <!-- .element: class="fragment" data-fragment-index="4" --></span> ---- 常用寫法 2 ```python with open([file_name], 'r') as [file_object]: for [line] in [file_object]: # do something ``` ```python with open('hello.txt', 'r') as f: for line in f: print(line, end='') ``` 遍歷所有列 ---- 常用寫法 3 ```python with open([file_name], 'w') as [file_object]: print([string], file = [file_object]) ``` ```python with open('bonjour.txt', 'w') as f: print('Bonjour, monde', file=f, flush=True) print("Je m'appelle Robert", file=f) ``` 這樣就不用寫`f.write`了(? 可以直接用`print` --- # 小練習 ---- 找出這個[檔案](https://gist.github.com/robert1003/0794860663de4177dd88e951132b2d21)裡最大的數字 找到了之後可以到[這裡](https://neoj.sprout.tw/problem/8763/)確認答案 --- # pickle ---- ![](https://i.imgur.com/n6HianH.jpg) 小黃瓜?? ---- 沒拉,pickle是一個直接存物件的東西 官網:[pickle](https://docs.python.org/3/library/pickle.html) ---- 怎麼使用? <span> ```python # pickle.dump([object_name], [file_object]) # [var_name] = pickle.load([file_object]) import pickle # dump, load dic = {1: 'a', 2: 'b', 3: 'c'} print(dic) with open('demo', 'wb') as f: pickle.dump(dic, f) with open('demo', 'rb') as f: dic2 = pickle.load(f) print(dic2) ``` <!-- .element: class="fragment" data-fragment-index="4" --></span> ---- 基本上任何python裡面的東西都可以用pickle存成一個二進位檔(binary file) --- # JSON ---- JSON (JavaScript Object Notation) 是一種人看得懂的存資料方法 官網:[JSON](https://www.json.org/json-en.html) ---- 如果把它當成python物件來看,他其實就只是由 list 跟 dict 還有 value 所組成 * `{}` 組成一個 dict * `[]` 組成一個 list * value 就是 字串、整數、true、false、null * dict 跟 list 可以互包 ---- 來看個例子: ![](https://i.imgur.com/3XeWgHY.png) ---- 如果你想挑戰自己,可以自己寫寫看 json parser XD 你可能會需要用到遞迴的觀念(我們沒教) ---- 如果你很懶也沒關係,我們來看看怎麼使用 `json` 這個套件~ ---- 我這邊示範的`sample.json`檔可以從[這裡](https://gist.githubusercontent.com/robert1003/4dbb3abb8b7d63aae1950f459ca7d4fa/raw/3ac196eb95d7034241bb676220aeb2df76d9ddf5/sample.json)下載~ ``` { "arguments": { "number": 10 }, "url": "http://localhost:8080/restty-tester/collection", "method": "POST", "header": { "Content-Type": "application/json" }, "body": [ { "id": 0, "name": "name 0", "description": "description 0" }, { "id": 1, "name": "name 1", "description": "description 1" } ], "output": "json", "delayed": false, "success": true, "ttl": 256.0, "secret": null } ``` ---- * `json.load`:從檔案讀 json * `json.loads`:從字串讀 json ```python # [var_name] = json.load([file_object]) # [var_name] = json.loads([string_name]) with open('sample.json', 'r') as f: obj = json.load(f) print(obj) with open('sample.json', 'r') as f: obj = json.loads(f.read()) print(obj) ``` ---- * `json.dump`:把 dict 或 list 換成 string 然後寫進檔案 * `json.dumps`:把 dict 或 list 換成 string ```python # json.dump([dict_or_list], [file_object]) # json.dumps([dict_or_list]) # a 就是 sample.json 讀出來的結果 a = {'arguments': {'number': 10}, 'url': 'http://localhost:8080/restty-tester/collection', 'method': 'POST', 'header': {'Content-Type': 'application/json'}, 'body': [{'id': 0, 'name': 'name 0', 'description': 'description 0'}, {'id': 1, 'name': 'name 1', 'description': 'description 1'}], 'output': 'json', 'delayed': False, 'success': True, 'ttl': 256.0, 'secret': None} with open('sample2.json', 'r') as f: json.dump(a, f, indent=4) # indent 參數可以不加,加了是為了排版 ㄖ s = json.dumps(a, indent=4) print(s) ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully