简体   繁体   中英

TypeError: argument should be string, bytes or integer, not PosixPath

My code works perfectly on my Raspberry Pi 4 system (Raspbian) but throws this strange error in a Petalinux system even though I'm using a string. The path exists and I'm logged in as root.

Code

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))

Error

[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: I already tried with '//root//web_gui//Recordings' but got the same error

Solved by updating Python to 3.6+. It appears that os.path.getctime function is a bit different in version 3.5

Quick Summary

This might be a problem with other code in your file. I've copied your code and it seems to work for me. (I've replaced some pathing with ... )

>>> str((sorted(Path('/Users/.../Desktop').iterdir(), key=os.path.getctime))[-1])
'/Users/.../Desktop/Programming'

What next?

  • Try adding more of your code to this question. I think you've correctly identified that this is a platform-specific issue.

Works fine on online Linux

>>> str((sorted(Path('/home').iterdir(), key=os.path.getctime))[-1])
'/home/user'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM