項目 widget-area-1 尚未註冊或是沒有一個 view.php 檔案.
項目 widget-area-1 尚未註冊或是沒有一個 view.php 檔案.
項目 search-input 尚未註冊或是沒有一個 view.php 檔案.

【Colab】Python colab 上傳檔案的方法 (內含範例程式碼) upload files

前言

colaboratory 是一個由 google 提供非常方便的網頁開發平台,
我們可以在上面進行 python 程式的快速開發,
而且最方便的是「python 環境」我們使用完就可以直接拋棄,
如果將「python 環境」裝在自己的電腦,
有時候「python 環境」壞了可能就很難修,甚至最慘可能要重灌電腦QQ。

為什麼要上傳檔案?

為什麼我們會需要上傳檔案? 畢竟 colaboratory 本身是雲端環境,
我們如果在「本地端」開發,能夠相對感受到的好處就是「檔案易於取用」,
我們透過上傳的方法。並直接將檔案串聯,就能彌補一些這方面的不方便。

註:如果你想要找的是「直接取用 google drive 資料的方法
主要差別是在「檔案能否在使用完 colab 後被保存下來」,請參考我的另外一篇文章

【Colab】Python colab 連接 google 雲端硬碟取用資料 (內含範例程式碼) mount google drive

上傳檔案範例

from google.colab import files
uploaded = files.upload()

file_name = list(uploaded.keys())[0]
print(file_name)

說明

  • uploaded = files.upload()
    這行就是我們進行上傳的功能,上傳完後我們會得到一個 dict (包含檔案相關資訊)
  • file_name = list(uploaded.keys())[0]
    這行是我們從剛剛的 dict 提取出檔案的名稱,在下一行我們就可以直接使用這個 path 去完成其他功能

Reference

https://ithelp.ithome.com.tw/articles/10217962

⭐Python 基礎用法 相關文章整理⭐:
1.【Python】python list 清除, 移除內容元素 remove, pop, del, clear相關用法整理 sample code (內含範例程式碼)
2.【Python】寫模組 module、package 總整理 Importing files from different folder
3.【Python】python assert (斷言) 用法 sample code (內含範例程式碼)
4.【Python】python 一行 if else 語法 (one line if else) sample code (內含範例程式碼)
5.【Python】lambda 與 def function 使用方法與比較整理(內含範例程式碼)
6.【Python】python map 使用方法 與 其他寫法比較整理 (內含範例程式碼) sample code
7.【Python】python zip 使用方法 與 其他寫法比較整理 (內含範例程式碼) sample code
⭐Python 字串處理 相關文章整理⭐:
1.【Python】python print 變數的值 & 變數名稱 方法總整理
2.【Python】python string format str.format 總整理
⭐Python 檔案處理 相關文章整理⭐:
1.【Python】python 開關檔範例 與 程式模板 with open / file open sample code
2.【Python】取出檔案名稱 (含副檔名、不含副檔名) os path basename split 取出 檔名 路徑 不要副檔名 sample code
3.【Python】在 python 中利用 os.chmod 更改檔案的權限 chmod 777
4.【Python】利用 shutil 來複製檔案 shutil copy file
5.【Python】python 建立資料夾範例 mkdir os.makedirs() sample code
6.【Python】python 移除資料夾範例 rmdir shutil.rmtree() sample code
7.【Python】確認檔案是否存在 os.path.isfile / 確認資料夾是否存在 os.path.isdir sample code is folder / file exist
⭐Python 系統偵測 相關文章整理⭐:
1.【Python】python pyinotify sample code 偵測指定路徑底下的文件變化 (內有範例程式碼)
2.【Python】python 利用 argparse 使程式執行時可帶參數 (內附範例程式碼) sample code
⭐Python 平行運算 相關文章整理⭐:
1.【Python】threading – 建立多執行緒來執行程式 (內含範例程式碼) sample code
2.【Python】multiprocessing – 01 | 用多核心來執行程式 (內含範例程式碼) sample code
3.【Python】multiprocessing – 02 | pool, map, apply_async – 用多核心來執行程式並取得結果 (內含範例程式碼) sample code
4.【Python】python thread multiprocess 比較總整理
5.【Python】python pooling multiprocess – 用多核心來執行