Answers for "read all files from a folder python"

8

python read a directory to get all files in sub folders

import os

path ="C:/workspace/python"
#we shall store all the file names in this list
filelist = []

for root, dirs, files in os.walk(path):
	for file in files:
        #append the file name to the list
		filelist.append(os.path.join(root,file))

#print all the file names
for name in filelist:
    print(name)
Posted by: Guest on November-19-2020
7

list files in directory python

import os
print(os.listdir('/path/to/folder/to/list'))
Posted by: Guest on April-22-2020
4

list of files in python

import os
def fn():       # 1.Get file names from directory
    file_list=os.listdir(r"C:\Users")
    print (file_list)

 #2.To rename files
fn()
Posted by: Guest on April-24-2020
-2

python read all text files in directory

import os
from re import search

arr = os.listdir()
strtxt = ".txt"
for txtfile in arr:
    if txtfile.__contains__(strtxt):
        fileObject = open(txtfile, "r")
        data = fileObject.read()
        print(data)
Posted by: Guest on December-22-2020

Code answers related to "read all files from a folder python"

Python Answers by Framework

Browse Popular Code Answers by Language