File System - Directory in Python
To get the directory name, it depends of the path type.
If this is:
dirname=os.path.dirname("/backup/remote_url.csv")
dirname
/backup
dirname=os.path.basename("/backup")
dirname
backup
What is the working or current directory ? in Python
import os
pwd = os.getcwd()
os.path.exists(directory)
os.makedirs(directory)
import os
def listdir_fullpath(d):
return [os.path.join(d, f) for f in os.listdir(d)]
import os
for dirname, dirnames, filenames in os.walk('.'):
# print path to all subdirectories first.
for subdirname in dirnames:
print(os.path.join(dirname, subdirname))
# print path to all filenames.
for filename in filenames:
print(os.path.join(dirname, filename))
# Advanced usage:
# editing the 'dirnames' list will stop os.walk() from recursing into there.
if '.git' in dirnames:
# don't go into any .git directories.
dirnames.remove('.git')