Below is a list of some important Python methods / functions with descriptions you should know to understand this article.
- len () — used to count the number of elements (elements / characters) of iterations like list, tuple, string, dictionary, etc.
- str () — used to convert a data value (integers, floats, list) to a string.
- abspath () — returns the absolute path to the file / directory name passed as an argument.
- enumerate () — returns an enumeration object for the passed iterable, which can be used to iterate over the elements of the iterable, accessing their indices.
- list () — used to create a list using an existing iteration (list, tuple, dictionary, set).
- listdir () — used to display the contents of a directory. The directory path is passed as an argument.
- isfile () — checks if the passed parameter denotes a file path. If yes, it returns True, otherwise False
- isdir () — checks if the passed parameter denotes a directory path. If yes, it returns True, otherwise False
- append () — is used to add items to a list.
Please see the code below, running in an interactive Python terminal, for a quick introduction to using the above functions / methods.
|
Path structure on different OS
Windows uses / (backslash) as path separator, eg. C: / Users / Desktop /
Linux based system like MAC OS X, Linux uses / (forward slash) for example. / Users / Desktop /
Let’s take a quick look at the above methods and functions as we use them in our final program.
|
Output:
5 9 [1, 3, 67, 45, 83, 59] [1, 3, 67, 45, 83, 59] A list. 2017 2017 A year. 0 1 1 3 2 67 3 45 4 83 5 59 / Users / admin / projects / Python / PythonFiles True True [’city’,’ age’, ’name’] [2, 34, 6, 8, 10] [2 , 34, 6, 8, 10, 98, 64] PythonPythonPython ###################
Inside / Users / admin / projects / Python / PythonFiles there are several Python files and directories, we will list them all.
Suppose the current working directory is / Users / admin / projects / Python / Django / E-Commerce-projects / ecommerce-2 / src which has several files and folders.
In your case, everything will be different. You will only need to provide the exact path to the directory you want to list.
Below is the Python code to list all files and directories based on the absolute or relative path passed.
If the path is not specified in the call statement, the contents of the current working directory will be displayed.
|
Output:
5 files of / Users / admin / projects / Python / Django / E-Commerce-projects / ecommerce-2 / src ========================== ================================================== ====== 1) .gitignore 2) db.sqlite3 3) manage.py 4) requirements.txt 5) todo.txt 5 directories of / Users / admin / projects / Python / Django / E-Commerce-projects / ecommerce-2 / src ============================================ ============================================ 1) ecommerce2 2) newsletter 3) products 4) static_in_pro 5) templates 70 files of / Users / admin / projects / Python / PythonFiles =========================== ========================= 1) 2_list_iterators.py 2) app.py 3) class_script_exec.py 4) class_variables.py 5) date_and_time.py 6) datetime.txt 7) dict.py 8) dictionary.py 9) django_home.html 10) error_handling.py 11) error_handling_output.py 12) error_handling_output.txt 13) execution_pickle.py 14) fb_task.py 15) for.py 16) gfg_sum_of_primes_in_numbers.py 17) hackerrank_numbers.py 18) hck_addition_aint_simple.py_chand) hanckpycountsed hck_c_counts2.py 22) hck_c_counts3.py 23) hck_cool_numbers.py 24) hck_earth_fans.py 25) hck_earth_fans_2.py 26) hck_earth_fans_3.py 27) hck_earth_fans_final_on_28_dec_2016.py 28) hck_Little_Jhool_and_psychic_powers.py 29) hck_lonely_monk.py 30) hck_lonely_monk_orig.py 31) hck_maximum_AND.py 32) hck_min_max_problem.py 33) hck_monk_and_power_of_time.py 34) hck_numbers_rotation.py 35) hck_palindomic_numbers.py 36) hck_print_hackeresive.py 37) hck_print_hackerepyarth_2_pyckery hck_strange_addition.py 42) hck_sum_of_numbers.py 43) interactive_img_resolutions.txt 44) json.py 45) json.pyc 46) katyperry.py 47) lambda_expression.py 48) linked_list_delete_nodes_at_front.p y 49) linked_list_delete_nodes_at_front_output.txt 50) linked_list_is_palindrome_gfg.py 51) linked_list_is_palindrome_gfg_output.text 52) linked_list_is_palindrome_gfg_testing.py 53) linked_list_node_deletion_from_any_position.txt 54) linked_list_node_deletion_from_end.py 55) linked_list_node_deletion_from_end_output.txt 56) linked_list_node_deletion_from_middle.py 57) linked_list_node_insertion_at_beginning.py 58) linked_list_node_insertion_at_end. py 59) linked_list_node_insertion_at_middel_output.txt 60) linked_list_node_insertion_at_middle.py 61) map.py 62) merge_lists.py 63) mufeez_android_interview.py 64) python_for_loops.py 65) python_for_loops2.pypl py 69) smarika_urllib_python2.7.10.py 70) while.py 2 directories of / Users / admin / projects / Python / PythonFiles ======================= ================================= 1) socket_programming 2) wx
Rishikesh Agravani strong>. If you are as Python.Engineering and would like to contribute, you can als
Listing directories and files in Python _files: Questions
Listing directories and files in Python File handling: Questions