Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.mkdir() returns error "FileNotFoundError: [Errno 2] No such file or directory"

Tags:

People also ask

How do I fix FileNotFoundError Errno 2 No such file or directory?

The Python "FileNotFoundError: [Errno 2] No such file or directory" occurs when we try to open a file that doesn't exist in the specified location. To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.

How do I fix error No such file or directory?

In some cases, this error could be shown when the path of the specified file or folders exceeds 258 characters in length. The way to solve this is to reduce the length of the full path to the items specified, either by moving or renaming the file(s) and/or containing folders.

How do you fix error number 2 in Python?

The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.

What is os mkdir in Python?

mkdir() method in Python is used to create a directory named path with the specified numeric mode. This method raise FileExistsError if the directory to be created already exists.


a very simple code to create a directory:

ssh_path = f"{os.getenv('HOME')}/temp/.ssh"
print(ssh_path)
os.mkdir(ssh_path)

it prints the path of directory and ends up with error

FileNotFoundError: [Errno 2] No such file or directory: '/Users/myusername/temp/.ssh'
/Users/myusername/temp/.ssh