![](/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.