TypeError: argument should be string, bytes or integer, not PosixPath
TypeError: argument should be string, bytes or integer, not PosixPath
我的代码在我的 Raspberry Pi 4 系统 (Raspbian) 上运行完美,但在 Petalinux 系统中抛出这个奇怪的错误,即使我使用的是字符串。该路径存在并且我以 root 身份登录。
代码
import os
from pathlib import Path
...
@app.route("/api/stopRecording")
def stopRecording():
recording_end_datetime = datetime.today().strftime('%m-%d-%Y-%H-%M-%S.mp4')
recordings_folder = '/home/pi/Documents/Recordings/'
f = open("run.pid", "r")
pidnum = int(f.readline())
os.system('kill -15 ' + str(pidnum))
video_filename = str((sorted(Path('/home/pi/Documents/Recordings').iterdir(), key=os.path.getctime))[-1])
if "---.mp4" in video_filename: # No stop timing has been set
os.system('mv "' + video_filename + '" "' + video_filename[:-4] + ' ' + recording_end_datetime + '"')
else: # Stop timing was set
os.system('mv "' + video_filename + '" "' + video_filename[:-24] + ' ' + recording_end_datetime + '"')
return jsonify({'response': 'recording off'})
@app.route("/api/loadRecordings")
def loadRecordings():
paths = sorted(Path('/home/pi/Documents/Recordings').iterdir(), key=os.path.getctime)
spaths = []
for p in paths:
spaths.append(str(p).replace('/home/pi/Documents/Recordings/','').replace('.mp4',''))
return jsonify(listToDict(spaths))
错误
[2020-06-03 21:01:55,591] ERROR in app: Exception on /api/loadRecordings [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "Backend/filetest.py", line 94, in loadRecordings
paths = sorted(Path('/root/web_gui/Recordings').iterdir(), key=os.path.getctime)
File "/usr/lib/python3.5/genericpath.py", line 65, in getctime
return os.stat(filename).st_ctime
TypeError: argument should be string, bytes or integer, not PosixPath
PS: 我已经尝试使用 '//root//web_gui//Recordings' 但出现了同样的错误
快速总结
这可能是您文件中其他代码的问题。我已经复制了你的代码,它似乎对我有用。 (我用 ...
替换了一些路径)
>>> str((sorted(Path('/Users/.../Desktop').iterdir(), key=os.path.getctime))[-1])
'/Users/.../Desktop/Programming'
下一步是什么?
- 尝试将更多代码添加到此问题中。我认为您已经正确地确定这是一个特定于平台的问题。
在 online Linux
上工作正常
>>> str((sorted(Path('/home').iterdir(), key=os.path.getctime))[-1])
'/home/user'
通过将 Python 更新到 3.6+ 解决!
看来 os.path.getctime 函数在 3.5
版本中有点不同
我的代码在我的 Raspberry Pi 4 系统 (Raspbian) 上运行完美,但在 Petalinux 系统中抛出这个奇怪的错误,即使我使用的是字符串。该路径存在并且我以 root 身份登录。
代码
import os
from pathlib import Path
...
@app.route("/api/stopRecording")
def stopRecording():
recording_end_datetime = datetime.today().strftime('%m-%d-%Y-%H-%M-%S.mp4')
recordings_folder = '/home/pi/Documents/Recordings/'
f = open("run.pid", "r")
pidnum = int(f.readline())
os.system('kill -15 ' + str(pidnum))
video_filename = str((sorted(Path('/home/pi/Documents/Recordings').iterdir(), key=os.path.getctime))[-1])
if "---.mp4" in video_filename: # No stop timing has been set
os.system('mv "' + video_filename + '" "' + video_filename[:-4] + ' ' + recording_end_datetime + '"')
else: # Stop timing was set
os.system('mv "' + video_filename + '" "' + video_filename[:-24] + ' ' + recording_end_datetime + '"')
return jsonify({'response': 'recording off'})
@app.route("/api/loadRecordings")
def loadRecordings():
paths = sorted(Path('/home/pi/Documents/Recordings').iterdir(), key=os.path.getctime)
spaths = []
for p in paths:
spaths.append(str(p).replace('/home/pi/Documents/Recordings/','').replace('.mp4',''))
return jsonify(listToDict(spaths))
错误
[2020-06-03 21:01:55,591] ERROR in app: Exception on /api/loadRecordings [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "Backend/filetest.py", line 94, in loadRecordings
paths = sorted(Path('/root/web_gui/Recordings').iterdir(), key=os.path.getctime)
File "/usr/lib/python3.5/genericpath.py", line 65, in getctime
return os.stat(filename).st_ctime
TypeError: argument should be string, bytes or integer, not PosixPath
PS: 我已经尝试使用 '//root//web_gui//Recordings' 但出现了同样的错误
快速总结
这可能是您文件中其他代码的问题。我已经复制了你的代码,它似乎对我有用。 (我用 ...
替换了一些路径)
>>> str((sorted(Path('/Users/.../Desktop').iterdir(), key=os.path.getctime))[-1])
'/Users/.../Desktop/Programming'
下一步是什么?
- 尝试将更多代码添加到此问题中。我认为您已经正确地确定这是一个特定于平台的问题。
在 online Linux
上工作正常>>> str((sorted(Path('/home').iterdir(), key=os.path.getctime))[-1])
'/home/user'
通过将 Python 更新到 3.6+ 解决! 看来 os.path.getctime 函数在 3.5
版本中有点不同