Answers for "python open directory and read files"

18

get files in directory python

import os
files_and_directories = os.listdir("path/to/directory")
Posted by: Guest on June-12-2020
5

get files in directory python

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
Posted by: Guest on November-27-2020
3

open file in python directory

path = 'C:\\Users\\Username\\Path\\To\\File'
file=open(path, "r")
Posted by: Guest on May-13-2020
0

python open directory and read files

import glob
for filename in glob.glob('*.txt'):
   with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
      # do your stuff
Posted by: Guest on April-20-2022
0

python open directory and read files

import os
for filename in os.listdir(os.getcwd()):
   with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
      # do your stuff
Posted by: Guest on April-20-2022
0

python open directory and read files

#It doesn't have to be the current directory you can list them in any path you want:
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
   with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
      # do your stuff
Posted by: Guest on April-20-2022

Code answers related to "python open directory and read files"

Python Answers by Framework

Browse Popular Code Answers by Language