簡體   English   中英

如何從python的路徑目錄中獲取.img文件?

[英]How can I get the .img file from the path directory in python?

我有一個擴展名為.img的文件,在我的操作系統/home/amanda/workspace/FX36.img這個路徑中。 我不知道如何在 python 中訪問那個 file.img 並在我的代碼中使用這個 file.img 作為主體。

        js_body = dict()
        file_path ="/home/amanda/workspace/FX36.img"
        img=open(file_path ,"r")
        js_body["File"] = img
        js_res = self._post(rest_uri, js_body)
        return js_res

我不知道我應該放什么來代替

img=open(file_path ,"r")

直到我有

js_body["File"] = FX36.img

如果有人可以幫助我,我將不勝感激。

img=open(file_path ,"r")

它只創建流。 您需要調用方法read()來獲取實際內容。

img = open(file_path, 'r').read()

如果您想要字節,請改用rb標志。

img_bytes = open(file_path, 'rb').read()

您還需要處理打開的流。

with open(file_path, 'r') as file:
    img = file.read()

我還建議不要在代碼中使用原始路徑。 它會在任何使用不同路徑約定的操作系統上崩潰。 請改用pathlib

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM