so I am done making a program that searches all the .txt files from a directory
I am further storing each words from those files into a list.
I was wondering if I can use import os to achieve the same goal.
Here's my working code with import glob
import glob
path = 'C:/Users/folder/PycharmProjects/firstproject/*.txt'
files = glob.glob(path)
for name in files:
with open(name, "r") as word_list:
words = word_list.read().split()
This is what I came up:
import os
from os.path import join
path = 'C:/Users/folder/PycharmProjects/firstproject/'
files = [join(path, f) for f in os.listdir(path)]
for name in files:
if name.endswith(".txt"):
with open(name, "r") as word_list:
words = word_list.read().split()
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.