Reading and writing

1670
12
Jump to solution
03-06-2021 12:18 AM
Mick
by
New Contributor
 
0 Kudos
1 Solution

Accepted Solutions
LanceCole
MVP Regular Contributor

Mick, 

Are you trying to crawl through a folder looking for files and their path?  I use the following to reclusively look for a particular file type in a folder and subfolders.  The function call below returns a list containing the folder and filename for all "txt" files in a directory and its subdirectors.  

import os, fnmatch

def locate(pattern, root=os.curdir):
  for folder, subs, files in os.walk(root):
    for filename in fnmatch.filter(files, pattern):
      yield os.path.join(folder, filename)

filelist = locate('*.txt', r'D:\path')

 

View solution in original post

0 Kudos
12 Replies
DavidPike
MVP Frequent Contributor

Python File Write (w3schools.com) - be very careful which mode you open the textfiles in, as this can delete them inadvertently.  e.g. 'w', 'a', 'ab'... I'd really recommend on reading up on them first.

#the os.path.join method is useful for joining paths
import os

#folder that texfiles will be saved in
myfolder = r'D:\Downloads'

#name of your textfiles
textfile_name_1 = 'demo1.txt'
textfile_name_2 = 'demo2.txt'

#concatenate(add together) path strings
textfile_path_1 = myfolder + '\\' + textfile_name_1
textfile_path_2 = myfolder + '\\' + textfile_name_2
#see the resulting paths
print(textfile_path_1)
print(textfile_path_2)

#or I prefer to use os.path.join
textfile_path_1 = os.path.join(myfolder, textfile_name_1)
textfile_path_2 = os.path.join(myfolder, textfile_name_2)
#see the resulting paths
print(textfile_path_1)
print(textfile_path_2)

#open/create a textfile and write to it
#the 'w' means write, but will also delete and
#recreate the entire textfile if already exists
f = open(textfile_path_1, 'w')
f.write('I have created a textfile and written this line to it')
f.close()

#the with statement means no .close() method is needed
with open(textfile_path_2, 'w') as text_file:
    text_file.write('I have created a tetxfile and written this line to it')
Mick
by
New Contributor

Unfortunately, I have more than one hundred files in the folder, so it is very difficult to indicate all of them. 

0 Kudos
DavidPike
MVP Frequent Contributor

What do you want to actually do?

0 Kudos
LanceCole
MVP Regular Contributor

Mick, 

Are you trying to crawl through a folder looking for files and their path?  I use the following to reclusively look for a particular file type in a folder and subfolders.  The function call below returns a list containing the folder and filename for all "txt" files in a directory and its subdirectors.  

import os, fnmatch

def locate(pattern, root=os.curdir):
  for folder, subs, files in os.walk(root):
    for filename in fnmatch.filter(files, pattern):
      yield os.path.join(folder, filename)

filelist = locate('*.txt', r'D:\path')

 

0 Kudos
DavidPike
MVP Frequent Contributor

wow you were ready for that one!

import os

my_folder = r'D:\Downloads'

file_list = []
#walk your folder
for root, dirs, files in os.walk(my_folder):
    
    for filename in files:
        #join filename to folder path
        filepath = os.path.join(my_folder, filename)
        #print to test the functionality
        print(filepath)
        #you can also append all the filepaths to a list, rather
        #than within this generator.
        #also added a test for .txt in filename
        if '.txt' in filename:
            
            file_list.append(filepath)
        
        
    break
0 Kudos
Mick
by
New Contributor

Yes, you are right. I have tried and received "line 9, in <module>
with open(filelist,"r") as f:
TypeError: coercing to Unicode: need string or buffer, generator found".

0 Kudos
LanceCole
MVP Regular Contributor

Mick, if you are referencing the filelist from Dave or my examples you iterate through the list using:

 

for fp in filelist:
  # fp is the full path and filename for each item in the filelist
  with open(fp,"r") as f:
    # do what you need here with the file data
    

 

Mick
by
New Contributor

I have a syntax error for "as" in        open(fp, "r") as f. 

0 Kudos
DavidPike
MVP Frequent Contributor
for fp in filelist:
  with open(fp, "r") as f:
    
0 Kudos