else:
statement that can be used after any loop (for
, while
, ...) statement. This else:
statement will be executed after the loop ends (for example, when the condition does meet anymore).if [1]:
print("Then")
else:
print("Else")
for x in [1]:
print("Then")
else:
print("Else")
os.path.normcase
, https://docs.python.org/2/library/os.path.html#os.path.normcase.os.path.normcase
is used to remove any unwanted properties and symbol from a path string. Hence, the string can be treated as a path.os.listdir()
, https://docs.python.org/2/library/os.html#os.listdir.os.listdir()
is used to return an array of all available files in path of this method's argument.os.remove()
used to remove a file.os.rmdir()
used to remove an empty directory.shutil.rmtree()
used to delete a directory and all its contents.os.path.isdir("bob")
for checking directory/folder.os.path.isfile("bob.txt")
for checking file (other than directory).os.path.basename(os.path.normpath('/folderA/folderB/folderC/folderD/'))
shutil
library, http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python.import shutil
# Both with or without the file extension works.
shutil.copy2('/dir/file.ext', '/new/dir/newname.ext')
shutil.copy2('/dir/file.ext', '/new/dir')
os
library. This does not working for moving file from internal storage into external storage in Unix based operating system. For that, use shutil.move()
instead, http://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python.os.rename("old.txt", "new.md")
those arguments can be an absolute path as well.shutil
library. This method works in both Unix and Windows operating system, http://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python.import shutil
shutil.move("old.txt", "new.md")
if not os.path.exists(directory): # Check first if the folder is exists.
os.makedirs(directory)
directory
could be just the name of the directory/folder you want to make or an absolute path.