# the list of files and foldersimport ostheList = os.listdir()print(theList)
# the list of files in current folderimport osfilenames =next(os.walk(os.getcwd()))[2]print(filenames)
# the list of files with given extensionimport globtxtfiles =[]for file in glob.glob("*.txt"): txtfiles.append(file)print(txtfiles)
# how to save the list of files in current folderimport osfilenames =next(os.walk(os.getcwd()))[2]print(filenames)f =open("theList.txt","w")for file in filenames: f.write(file) f.write('\n')f.close()
Jak stworzyć lub usunąć folder w python?
# create folderimport osifnot os.path.exists("myfolder"): os.mkdir("myfolder")else:print("Folder is created already")
# delete folder if exists and emptyimport osif os.path.exists("myfolder"): os.rmdir("myfolder")else:print("Folder doesn't exist")