![](/img/trans.png)
[英]Check if files are open (because of error: “text file busy”) in Python
[英]Python: .png image file that wont open because of text before file signature
我有大量的 png 文件,在 ‰PNG 文件簽名之前添加了一些文本。 我正在嘗試找到一種方法來打開 python 中的文件並在寫入新文件之前刪除多余的文本,以便我可以查看圖像。
下圖中可以看到來自 Notepad++ 的文件截圖,以便更好地理解問題。
到目前為止,我已經嘗試過這段代碼
infile = open('radar0.1.107652907', encoding='ANSI')
outfile = open('test.png', 'w', encoding='ANSI')
imagetext = infile.read()
pos = imagetext.find('‰')
outtext = imagetext[pos:]
outfile.write(imagetext)
但是當我嘗試打開新文件時,它不會打開。
任何幫助將不勝感激
我能夠使用@BoarGules 建議以二進制方式解決問題。 代碼如下
infile = open('radar0.1.107652907', 'rb')
outfile = open('test.png', 'wb')
imagetext = infile.read()
pos = imagetext.find(b'\x89PNG')
outtext = imagetext[pos:]
outfile.write(outtext)
infile.close()
outfile.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.