亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試請求文件輸入并將其保存在特定位置

嘗試請求文件輸入并將其保存在特定位置

MMTTMM 2023-10-31 21:29:37
所以我試圖編寫一種方法來保存用戶選擇的圖片并將圖片添加到父類“activity”中。這是我嘗試過的:    # Creating object from filedialog    picture = filedialog.asksaveasfile(mode='r', title='Select activity picture', defaultextension=".jpg")        # Check if cancelled    if picture is None:        return    else:        folder_dir = 'main/data/activity_pics'        pic_name = 'rocket_league' #change to desc_to_img_name() later        path = os.path.join(folder_dir, f'{pic_name}')                # Making the folder if it does not exist yet        if not os.path.exists(folder_dir):            os.makedirs(folder_dir)                    # Creating a location object        f = open(path, "a")                # Writing picture on location object        f.write(picture)                # Closing        f.close()        # Check if successful        if os.path.exists(path):            print(f'File saved as {pic_name} in directory {folder_dir}')            self.picture_path = path后來,我使用 調用該方法rocketleague.add_picture(),其中我將 Rocketleague 定義為一個Activity類。我嘗試了在網上找到的幾種不同的解決方案,但似乎都不起作用。目前,該腳本出現錯誤:C:\Users\timda\PycharmProjects\group-31\venv\Scripts\python.exe C:/Users/timda/PycharmProjects/group-31/project/activities/random_activity_generator.pyTraceback (most recent call last):  File "C:/Users/timda/PycharmProjects/group-31/project/activities/random_activity_generator.py", line 160, in <module>    rocketleague.add_picture()  File "C:/Users/timda/PycharmProjects/group-31/project/activities/random_activity_generator.py", line 46, in add_picture    f.write(picture)TypeError: write() argument must be str, not _io.TextIOWrapper所以我猜我的 open.write() 除了字符串之外不適合任何東西。我不確定我當前的方法是否有效。執行此操作最方便的方法是什么?
查看完整描述

1 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

根據您的描述,您正在使用完全不同的方法來完成任務。

我將首先解釋您想要做什么以及為什么它不起作用。

-> 因此,在您要求用戶選擇一個文件并創建folder_dirpath后,您希望將所選圖片移動/復制到變量 描述的位置path。

問題,

  • 根據您的需要,您可能就在這里,但我覺得您應該使用filedialog.askopenfile()而不是filedialog.asksaveasfile(),因為您可能希望他們選擇一個文件,而不是選擇一個文件夾來將文件移動到 - 您似乎已經有了目的地folder_dir。但這又取決于您的需求。

  • 重要的一個。在您的代碼中,其中一條注釋顯示:“”“創建位置對象”“”您使用的f = open(path, "a"),您沒有在那里創建位置對象。f只是一個file以文本追加模式打開的對象 - 如函數調用"a"中的第二個參數所示open()。您無法將二進制圖像文件寫入文本文件。這就是為什么錯誤消息說它期望將字符串寫入文本文件,而不是二進制 I/O 包裝器。

解決方案

很簡單,使用實際方法移動文件來執行任務。

因此,一旦您有了圖像文件的地址(由用戶在filedialog上面解釋的中選擇),請注意,正如@Bryan 指出的那樣,picture變量將始終file handler是用戶選擇的文件的地址。.name我們可以使用.txt文件的屬性來獲取所選文件的絕對地址file handler。

>>> import tkinter.filedialog as fd

>>> a = fd.askopenfile()

# I selected a test.txt file


>>> a

<_io.TextIOWrapper name='C:/BeingProfessional/projects/Py/test.txt' mode='r' encoding='cp1252'>


# the type is neither a file, not an address.

>>> type(a)

<class '_io.TextIOWrapper'>

>>> import os


# getting the absolute address with name attribute

>>> print(a.name)

C:/BeingProfessional/projects/Py/test.txt

# check aith os.path

>>> os.path.exists(a.name)

True

注意:

我們還可以使用filedialog.askopenfilename()它僅返回所選文件的地址,這樣您就不必擔心文件處理程序及其屬性。


我們已經有了目的地folder_dir和要給出的新文件名。這就是我們所需要的,源和目的地。這是復制文件的操作


這三種方法完成相同的工作。根據需要使用其中任何一個。


import os

import shutil


os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo")

shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")

os.replace("path/to/current/file.foo", "path/to/new/destination/for/file.foo")

請注意,您必須在源參數和目標參數中包含文件名 (file.foo)。如果更改,文件將被重命名并移動。


另請注意,在前兩種情況下,創建新文件的目錄必須已經存在。在 Windows 計算機上,具有該名稱的文件一定不存在,否則exception將會引發錯誤,但os.replace()即使在這種情況下,也會默默地替換文件。


您的代碼已修改

# Creating object from filedialog

picture = filedialog.askopenfile(mode='r', title='Select activity picture', defaultextension=".jpg")


# Check if cancelled

if picture is None:

    return

else:

    folder_dir = 'main/data/activity_pics'

    pic_name = 'rocket_league' #change to desc_to_img_name() later

    path = os.path.join(folder_dir, f'{pic_name}')

    

    # Making the folder if it does not exist yet

    if not os.path.exists(folder_dir):

        os.makedirs(folder_dir)

        

   # the change here

   shutil.move(picture.name, path)  # using attribute to get abs address


    # Check if successful

    if os.path.exists(path):

        print(f'File saved as {pic_name} in directory {folder_dir}')

        self.picture_path = path

該代碼塊的縮進不是很好,因為原始問題格式的縮進沒有那么好。


查看完整回答
反對 回復 2023-10-31
  • 1 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號