Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing list to pc.uploadfile gives exception #69

Closed
ikwyl6 opened this issue Nov 15, 2022 · 1 comment
Closed

Passing list to pc.uploadfile gives exception #69

ikwyl6 opened this issue Nov 15, 2022 · 1 comment

Comments

@ikwyl6
Copy link

ikwyl6 commented Nov 15, 2022

In my script I use argparse to accept a '--file' parameter where I can specify a single or multiple files to upload to pcloud:

clp.add_argument('-f', '--file', type=argparse.FileType('r'), nargs='+',
                  help='List of local FILEs to upload')
clargs = clp.parse_args()

by running it like so:

./pcloud-upload.py -f ./README.md ./LICENSE

and when this happens, my script passes clargs.file (a python list) to uploadfile in pycloud but throws following exception(s):

Traceback (most recent call last):
  File "/home/alarm/dev/pcloud-upload/./pcloud-upload.py", line 227, in <module>
    fileinfo = pc.uploadfile(files=[pc_uploadlist], path='/') # folderid=folderid) # path=upload_dir)
  File "/home/alarm/.local/lib/python3.10/site-packages/pcloud/validate.py", line 19, in wrapper
    return func(*args, **kwargs)
  File "/home/alarm/.local/lib/python3.10/site-packages/pcloud/api.py", line 265, in uploadfile
    files = [
  File "/home/alarm/.local/lib/python3.10/site-packages/pcloud/api.py", line 266, in <listcomp>
    ("file", (os.path.split(f)[1], open(f, "rb"))) for f in upload_files
  File "/usr/lib/python3.10/posixpath.py", line 103, in split
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not list

I thought it might have been the argparse.FileType('r') was the problem with an improper cast to a list, which is what I believe pycloud will accept for more than one file. But when I removed argparse.FileType('r') from the argparse part of the script, I got the same exception(s). To remove the issue with perhaps argparse being the problem, I hardcoded a list to be sent to pc.uploadfile like so:

upload_files = ['./README.md', './LICENSE']
pc_uploadlist = upload_files
fileinfo = pc.uploadfile(files=[pc_uploadlist], path='/')

and I get the exact same set of exceptions as above.

In your uploadfile function https://github.com/tomgross/pycloud/blob/master/src/pcloud/api.py#L251 you go through your kwargs files and pop off each item in the list but I'm not sure if this is working properly for accepting a list for uploadfile.

In my script, before I used argparse, I was just passing my --file command line option using a variable and pcloud would upload it properly. So I feel that the api's uploadfile function is not accepting a list and processing that list properly.

Testing some more - I only pass a single file to argparse like so ./pcloud-upload.py -f ./README.md and changing my script line above from:

fileinfo = pc.uploadfile(files=[pc_uploadlist], path='/')

to

fileinfo = pc.uploadfile(files=clargs.file, path='/') 

works properly and uploaded the single file. But I cannot pass two files to my script.

@tomgross
Copy link
Owner

It seems you are passing a list of lists to files:

According to your example

fileinfo = pc.uploadfile(files=[pc_uploadlist], path='/')

translates to

fileinfo = pc.uploadfile(files=[['./README.md', './LICENSE']], path='/')

Remove the square brackets and the uploadfile-method should work as expected.

fileinfo = pc.uploadfile(files=pc_uploadlist, path='/')

If you use and ❤️ my software, consider buying me a coffee ☕ https://www.paypal.com/paypalme/tomgross42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants