xxxxxxxxxx
#Reads a specified .txt file and prints all of it's contents.
#Use [FolderName] and then a / to show the code where the file is.
with open('FolderName/FileName.txt') as myFile:
print(someText.read())
xxxxxxxxxx
from pathlib import Path
for txt_path in Path("/path/folder/directory").glob("*.txt"):
print(txt_path)
xxxxxxxxxx
#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
xxxxxxxxxx
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
xxxxxxxxxx
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
xxxxxxxxxx
# Basic syntax:
import glob
list_of_files = glob.glob("search_pattern")
# Where:
# - the search pattern can be any glob pattern, e.g. /usr/bin/*py* to get the
# list of files in /usr/bin that contain py
xxxxxxxxxx
(_, _, filenames) = os.walk(mypath).next() #if you are confident that the walk will return at least one value