Skip to main content

Posts

Showing posts from May, 2018

Get the list of all the files and directories

Function Use: To print all the directories in a folder/sub-folder or directory import os def get_list_or_dir(local_dir):     for dir_or_file in os.listdir(local_dir):         if os.path.isdir(dir_or_file):             path = os.path.join(local_dir, dir_or_file)             print(dir_or_file, 'This is a directory')             get_list_or_dir(path)         else:             print(dir_or_file) Path = os.getcwd() or define the path from where you need a file Function Call: get_list_or_dir(path)

Use of end=" " parameter in Python

We already know that in python the print() function ends with a new line.  For example, if we the try to execute the below program,  python IDE will take us automatically to the new line.  >>> print("Hello! Python") Hello! Python >>>  I will try to explain this with one more example. >>> for i in range(1,5): print(i) 1 2 3 4 >>>  In the above program, we can see that each time the print(i) statement gets executed, by default python takes us to the new line.  What if, we want to display the above result horizontally or row wise.  Like "1 2 3 4". end=" " makes it possible for us to display the results horizontally. >>> for i in range(1,5): print(i, end=" ") 1 2 3 4  >>>  We have just added end=" " parameter in the print statement and the results are displayed in the horizontal.  We can use anything inside the e...