def dezip(): now = datetime.now() current_time = now.strftime("%Y-%m-%d-%H-%M-%S") target_folder = os.makedirs(os.path.join(r'D:/backup', current_time)) files = request.files.getlist('file') for file in files: filename = file.filename file_path = os.path.join(target_folder, filename) file.save(file_path) return jsonify({"code": 200, "msg": "success"}) 出现这种错误 path = os.fspath(path) TypeError: expected str, bytes or os.PathLike object, not NoneType 如何改代码
时间: 2024-04-14 08:26:16 浏览: 33
在你的代码中,出现了 "TypeError: expected str, bytes or os.PathLike object, not NoneType" 错误。这是因为 `os.makedirs()` 函数返回 `None`,而不是一个路径字符串。
为了解决这个问题,你可以将 `os.makedirs()` 与 `os.path.join()` 分开,并将路径字符串赋值给 `target_folder` 变量。以下是修改后的代码示例:
```python
from datetime import datetime
import os
@app.route('/dezip', methods=['POST'])
def dezip():
now = datetime.now()
current_time = now.strftime("%Y-%m-%d-%H-%M-%S")
target_folder = os.path.join(r'D:/backup', current_time)
os.makedirs(target_folder) # 创建目标文件夹
files = request.files.getlist('file')
for file in files:
filename = file.filename
file_path = os.path.join(target_folder, filename)
file.save(file_path)
return jsonify({"code": 200, "msg": "success"})
```
在修改后的代码中,首先使用 `os.path.join()` 构建目标文件夹的路径字符串,并将其赋值给 `target_folder` 变量。
然后,使用 `os.makedirs()` 函数来创建目标文件夹。
接下来,你可以继续处理接收到的文件,并将它们保存到目标文件夹中。
请确保文件路径的正确性和安全性,并根据需要进行适当的修改。
希望这可以帮助你解决问题!如果还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)