31
loading...
This website collects cookies to deliver better user experience
import os
print(os.getcwd())
# C:\Users\AyaBouchiha\Desktop\posts
import os
os.mkdir('python') # done :)
os.mkdir('python') # error (FileExistsError)
import os
# done
os.mkdir('python/newFolder')
# done
os.rmdir('python/newFolder')
# error (FileNotFoundError)
os.rmdir('something/does-not-exists')
# error (NotADirectoryError)
os.rmdir('user.json')
import os
# done
os.remove('posts/python/test.py')
# error(FileNotFoundError)
os.remove('posts/python/test.py')
import os
my_file_path = 'posts/python/os_module.py'
# True
print(os.path.isfile(my_file_path))
file_does_not_exists_path = 'posts/python/os_module.json'
# False
print(os.path.isfile(file_does_not_exists_path))
directory_path = 'posts/python'
# False
print(os.path.isfile(directory_path))
import os
print(os.listdir('posts/'))
# Output:
# [
# '5_list_python_methods.md',
# 'js_resources.md',
# 'MapObject.md',
# 'os_module.md',
# 'os_module.py',
# 'python'
# ]
getcwd(): returns the absolute path of the current working directory as a string.
mkdir(): creates a new directory.
rmdir(): removes an empty directory.
remove(): deletes a file.
isfile(): returns True if the given path is a regular file.
listdir(): returns a list that contains all files and directories names that exist in the specified path.