watch_folder = r'D:/gangko/zip' upload_url = 'http://192.168.1.226:6060/dz' class FileEventHandler(FileSystemEventHandler): def on_created(self, event): if not event.is_directory: file_path = event.src_path with open(file_path, 'rb') as file: files = {'file': file} response = requests.post(upload_url, files=files) print(f"Uploaded {file_path}: {response.status_code}") if __name__ == "__main__": event_handler = FileEventHandler() observer = Observer() observer.schedule(event_handler, watch_folder, recursive=True) try: observer.start() while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() 为什么这段代码拷贝的压缩文件都损坏了
时间: 2024-04-16 09:25:33 浏览: 115
根据你提供的代码,我看不出为什么拷贝的压缩文件会损坏。这段代码是一个简单的文件监视器,当在 `watch_folder` 目录下创建新文件时,它会将文件上传到 `upload_url` 指定的地址。没有看到对文件进行压缩或解压缩的操作。
如果你发现拷贝的压缩文件都损坏了,可能是其他部分的代码或操作导致的问题。请确保以下几点:
1. 确保文件在拷贝过程中没有被修改或损坏。可以尝试手动拷贝文件并检查是否损坏。
2. 确保上传到 `upload_url` 的代码没有对文件进行修改、损坏或解压缩操作。
3. 检查 `upload_url` 目标地址和接收文件的服务是否正常工作,并且能够正确处理接收到的文件。
如果问题仍然存在,我建议你检查其他与文件拷贝、压缩或上传相关的代码或操作。可能有其他部分的代码导致了文件损坏的问题。
希望能帮到你!如果还有其他问题,请随时提问。
相关问题
watch_folder = r'D:/zip' upload_url = 'http://192.168.1.231:6060/dz' def upload_file(file_path): with open(file_path, 'rb') as file: files = {'file': file} response = requests.post(upload_url, files=files) print(f"Uploaded {file_path}: {response.status_code}") def delete_file(file_path): os.remove(file_path) print(f"Deleted {file_path}") def monitor_folder(): files = set() while True: current_files = set(os.listdir(watch_folder)) new_files = current_files - files for file in new_files: file_path = os.path.join(watch_folder, file) upload_file(file_path) delete_file(file_path) files = current_files time.sleep(15) if __name__ == "__main__": monitor_folder() 这段代码一段时间没有接收到连接会报错, raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.1.231', port=6060): Max retries exceeded with url: /dz (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001915C2540F0>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',)) 如何改代码可以在没有接收到连接时一直等待,而不是报错中断程序
要在没有接收到连接时一直等待而不报错中断程序,您可以使用 `try-except` 块来捕获连接错误,并在捕获到错误时进行延迟重试。下面是修改后的代码示例:
```python
import os
import time
import requests
watch_folder = r'D:/zip'
upload_url = 'http://192.168.1.231:6060/dz'
def upload_file(file_path):
with open(file_path, 'rb') as file:
files = {'file': file}
response = requests.post(upload_url, files=files)
print(f"Uploaded {file_path}: {response.status_code}")
def delete_file(file_path):
os.remove(file_path)
print(f"Deleted {file_path}")
def monitor_folder():
files = set()
while True:
current_files = set(os.listdir(watch_folder))
new_files = current_files - files
for file in new_files:
file_path = os.path.join(watch_folder, file)
try:
upload_file(file_path)
delete_file(file_path)
except requests.exceptions.ConnectionError as e:
print(f"Connection error: {e}")
time.sleep(15) # 等待一段时间后重试
files = current_files
time.sleep(15)
if __name__ == "__main__":
monitor_folder()
```
在修改后的代码中,当捕获到 `ConnectionError` 异常时,会打印错误消息并等待一段时间后重试上传。这样可以避免报错中断程序,并持续等待连接恢复。
from PIL import Image import os import numpy as np input_folder = 'D://with ground truth/train/disp' # 输入文件夹路径 output_folder = 'D://with ground truth/train/disp2' # 输出文件夹路径 if not os.path.exists(output_folder): os.makedirs(output_folder) for filename in os.listdir(input_folder): if filename.endswith('.tif') or filename.endswith('.tiff'): # 仅对tif或tiff格式的文件进行转换 with Image.open(os.path.join(input_folder, filename)) as im: im.save(os.path.join(output_folder, os.path.splitext(filename)[0] + '.png'))报错raise OSError(msg) from e OSError: cannot write mode F as PNG
这个错误通常会出现在尝试以模式 "F" 保存 PNG 图像时。模式 "F" 表示浮点数像素,但 PNG 格式不支持浮点数像素。因此,你需要将图像转换为支持的模式(例如 "RGB" 或 "RGBA")才能将其保存为 PNG。以下是一个示例代码片段,可以将图像转换为 "RGB" 模式并保存为 PNG:
```
from PIL import Image
import os
import numpy as np
input_folder = 'D://with ground truth/train/disp' # 输入文件夹路径
output_folder = 'D://with ground truth/train/disp2' # 输出文件夹路径
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(input_folder):
if filename.endswith('.tif') or filename.endswith('.tiff'): # 仅对tif或tiff格式的文件进行转换
with Image.open(os.path.join(input_folder, filename)) as im:
# 将图像转换为 "RGB" 模式
im = im.convert("RGB")
# 保存为 PNG
im.save(os.path.join(output_folder, os.path.splitext(filename)[0] + '.png'))
```
如果你仍然遇到问题,请提供完整的错误消息。
阅读全文