When I run the code it says "TypeError: unlink() got an unexpected keyword argument 'missing_ok'"

Viewed 2711

This is my code. It is everything I have in my programme:

from pathlib import Path
new_dir = Path.home() / "new_directory"
file_path = new_dir / "program2.py"
file_path.unlink(missing_ok=True)

The file program2.py does not exist; that is why I wanted to set the missing_ok parameter to True so that it would not raise a FileExistsError. But every time I run the code it gives me the following message:

file_path.unlink(missing_ok=True)
TypeError: unlink() got an unexpected keyword argument 'missing_ok'

Do I have an outdated version of python or have I made a mistake in the code, help would be much appreciated!

2 Answers

The missing_ok parameter was added to Path.unlink only on python 3.8. You should upgrade python to newer version if you want to use this parameter.

You can check your python version with the command python -V

TypeError: unlink() got an unexpected keyword argument 'missing_ok'

this means that the unlink() function is not expecting your input variable_name missing_ok

It also looks like you're trying to do [numerical] division in these two lines (which is not a valid operation...?)

new_dir = Path.home() / "new_directory"
file_path = new_dir / > "program2.py"
Related