执行Python代码时出现TypeError: j1()缺少必填位置参数'its'的问题排查
The error you're seeing is straightforward: your j1() function is defined to accept 4 positional arguments, but when you call it in the /remove branch, you're only passing 3. On top of that, the order of the arguments you are passing doesn't align with what the function expects, which would cause further issues even if you added the missing parameter.
Let's break down the mismatch:
Function definition:
def j1(localpath, path, file, its)expects these four parameters in order:localpath: Local file path (used only in the 'upload' case)path: Remote file path (used in 'remove' forsftp.remove())file: Filename for logs/messagesits: Action type ('upload' or 'remove')
Your call:
j1(path + '/' + file, file, its)only passes 3 arguments, skipping the second parameter (path), and misaligning the rest.
The Fix
Adjust the call to pass all four arguments in the correct order. Since localpath isn't used in the 'remove' case, you can pass an empty string (or any placeholder value) for that parameter. Don't forget to fix the calls to j2() and j3() too, as they likely have the same signature:
if commanda == '/remove': if mode == 'jail': # Pass all 4 required parameters in the correct sequence j1("", path + '/' + file, file, its) j2("", path + '/' + file, file, its) j3("", path + '/' + file, file, its) update()
Optional Improvement
To avoid this kind of mistake in the future, you could modify the j1() function to make localpath an optional parameter (with a default value). Since it's only used in the 'upload' case, rearrange the parameters to put optional ones at the end:
# Revised function definition with optional localpath def j1(path, file, its, localpath=""): if its == 'upload': sftp.put(localpath, path) sender(xxx, 'JAIL-1: File uploaded successfully!' + ' [' + file + ' -> ' + path + ']') mode = 'JAIL-1' log(mode, file, path) if its == 'remove': sftp.remove(path) sender(526891231, 'JAIL-1: File deleted successfully!' + ' [' + path + ']') mode = 'JAIL-1' log(mode, file, path)
Then, your 'remove' calls can omit the localpath parameter entirely:
j1(path + '/' + file, file, its)
And your 'upload' calls would just add localpath as the fourth argument:
j1(remote_path, file, 'upload', local_file_path)
This makes the code more robust and self-documenting.
内容的提问来源于stack exchange,提问作者COLDER

