drilling down into dirs\sub-dirs looking for specific file names - python

drilling down into sub-dirs looking for specific files to modify.
Using glob.glob() to search all sub-dirs for files.
path = 'C:\Test\*\*[1234,349,4568]*.*'
#print(path)
files = glob.glob(path)
print(files)
for name in files:
with open(name,'r') as inputfile:
newText = inputfile.read().replace('5484522-102','P/N 545616-102')
print(newText)
with open(name, "w") as outputfile:
outputfile.write(newText)
print('Done !')
modifies the files called out in the path and many more I don't want to be modified. How do i modify only the files called out in the path?

#!/usr/bin/env python3
#This code finds the specific files listed in a dir and copies them to another dir.
There the files are read and the p/n replace with another p/n
import os
import shutil
import glob
import pandas as pd
#fill in the file name into this set
df = pd.read_csv('c:\Test\ReadFiles.csv')
path = 'C:\Test\Test\*.*'
dest_dir = 'C:\Test\Test' # New dir for the files found
src_dir = 'C:\Test' # Search dir
walking through the dir and files looking for the files in the set().
for (dirpath, dirnames, filenames) in os.walk(src_dir):
for fname in filenames:
if fname[:6] in df:
print(fname)
shutil.copy(os.path.join(dirpath, fname), dest_dir)
iterate through files found and change the part number
files = glob.glob(path)
print(files)
for name in files:
with open(name,'r') as inputfile:
newText = inputfile.read().replace('222222-101','111111-101')
with open(name, "w") as outputfile:
outputfile.write(newText)
print(outputfile)
print('Done !')

Related

saving file names into a txt file and copying files into a directory query

-I tried the script below but the script is copying batch file names and pythonw.exe into the found_file.txt file and also it is not displaying anything in the found files folder. If anyone would look into it and help me out, I'd appreciate it.
The code is below.
import shutil
path = 'C:'
FileList = []
extension = '.pdf' and '.docx' and '.jpeg'
# Scanning For Files :-
with os.scandir(path) as DirList:
for root, dirs_list, files_list in os.walk(path):
for file_name in files_list:
if os.path.splitext(file_name)[-1] == extension:
file_name_path = os.path.join(root, file_name)
for files in DirList:
if files.is_file():
FileList.append(files.name)
# After that it iterates over the FileList and write names to text file
with open("found_file.txt", 'w') as ffile:
for fname in files_list:
ffile.write(fname)
#copies all files into a folder called found_files
newPath = shutil.copy('files_list', 'C:/Users/PacY/Downloads/found_files')
the immediate action would be this:
import shutil
path = 'C:'
FileList = []
extensions = ['.pdf', '.docx', '.jpeg']
# Scanning For Files :-
with os.scandir(path) as DirList:
for root, dirs_list, files_list in os.walk(path):
for file_name in files_list:
# this loops over each extensions and evaluates it
for extension in extensions:
if os.path.splitext(file_name)[-1] == extension:
file_name_path = os.path.join(root, file_name)
# break here so that if matching extension is found it stops checking
break
for files in DirList:
if files.is_file():
FileList.append(files.name)
# After that it iterates over the FileList and write names to text file
with open("found_file.txt", 'w') as ffile:
for fname in files_list:
ffile.write(fname)
#copies all files into a folder called found_files
newPath = shutil.copy('files_list', 'C:/Users/PacY/Downloads/found_files')
You should learn about this because those are the basics. also if the code does not work feel free to ask questions. also isn't there a specific function for this? (not that I would know)

Combine csv files with same name from different subfolders in to one csv

I have three CSV files each for a particular filename for multiple files. Let's say there are a total 20 filenames so total 20* 3csv files in three different folders.
Folder A- 1001.CSV,1002.CSV,1003.CSV...
Folder B-1001.CSV,1002.CSV,1003.CSV
Folder C-1001.csv,1002.csv,1003.csv......
I want to get a single CSV file for each 1001,1002,1003,1004.....
So total 20csv files
How can I do this? Since the files are in different folders glob is not working(or I don't know how to)
I made the following assumptions:
all the subfolders will be rooted at some known directory "parentdir"
each subfolder contains only relevant csv files
the csv files do not contain any header/footer lines
each record in the csv files is separated by a newline
all of the records in each file are relevant
This should produce a "concat.csv" file in each subfolder with the contents of all the other files in that same folder. I used a snippet of code from this other answer on stackoverflow for actually concatenating the files.
import os
import fileinput
rootdir = 'C:\\Users\\myname\\Desktop\\parentdir'
os.chdir(rootdir)
children = os.listdir()
for i in children:
path = os.path.join(rootdir, i)
os.chdir(path)
filenames = os.listdir()
with open('concat.csv', 'w') as fout, fileinput.input(filenames) as fin:
for line in fin:
fout.write(line + '\n')
import os
import shutil
import glob
import pandas as pd
path = '/mypath/'
# rename files
count = 1
for root, dirs, files in os.walk(path):
for i in files:
if i == 'whatever.csv':
os.rename(os.path.join(root, i), os.path.join(root, "whatever" + str(count) + ".csv"))
count += 1
# delete unwanted files
main_dir = path
folders = os.listdir(main_dir)
for (dirname, dirs, files) in os.walk(main_dir):
for file in files:
if file.startswith('dontwant'):
source_file = os.path.join(dirname, file)
os.remove(source_file)
# copy files to dir
for root, dirs, files in os.walk(path): # replace the . with your starting directory
for file in files:
if file.endswith('.csv'):
path_file = os.path.join(root,file)
shutil.copy2(path_file,path) # change you destination dir
# combine files
os.chdir(path)
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')

find string line startswith recursively python

i have to find recursively all lines( which start with string "excel") in all files (in directory and subdirectory) .i need for each filename the line found (for example :
filename1:
line1 founded...
filename2:
line2 founded...
Output result in file called "logfile"
if no line founded , filename not saved in logfile.
import os
word="excel"
from os.path import join
for (dirname, dirs, files) in os.walk('/batch/'):
for filename in files:
thefile = os.path.join(dirname,filename)
for line in files:
if line.startswith(word):
print (line)
print (thefile)
Thanks
Your code just has minor problems: The biggest one is that you loop on filename instead of file content.
import os
word="excel"
from os.path import join
for (dirname, dirs, files) in os.walk('/batch/'):
for filename in files:
thefile = os.path.join(dirname, filename)
with open(thefile) as f:
for line in f:
if line.startswith(word):
print (line)
print (thefile)
EDIT:
import os
word="excel"
from os.path import join
with open('log_result.txt', 'w') as log_file:
for (dirname, dirs, files) in os.walk('/tmp/toto'):
for filename in files:
thefile = os.path.join(dirname, filename)
with open(thefile) as f:
lines = [line for line in f if line.startswith(word)]
if lines:
log_file.write("File {}:\n".format(thefile))
log_file.writelines(lines)
Here is the fixed code.
you don't need to re traverse the same list of files.
os.walk() will return all sub directories in a directory, All you need you do is loop all directories.
Sample Code
import glob
import os
word="excel"
for (dirname, dirs, files) in os.walk("/batch/"):
for file_ in files :
if file_.startswith(word):
print(file_)
print(os.path.join(dirname, file_))
for dir_ in dirs :
myfiles = glob.glob(os.path.join(dirname,dir_))
for myfile in myfiles:
if myfile.startswith(word):
print(myfile)
print(os.path.join(dirname,myfiles))
hope this helps

read contents of a file from a list of file with os.listdir() (python)

I need to read the contents of a file from the list of files from a directory with os.listdir. My working scriptlet is as follows:
import os
path = "/Users/Desktop/test/"
for filename in os.listdir(path):
with open(filename, 'rU') as f:
t = f.read()
t = t.split()
print(t)
print(t) gives me all the contents from all the files at once present in the directory (path).
But I like to print the contents on first file, then contents of the second and so on, until all the files are read from in dir.
Please guide ! Thanks.
You can print the file name.
Print the content after the file name.
import os
path = "/home/vpraveen/uni_tmp/temp"
for filename in os.listdir(path):
with open(filename, 'rU') as f:
t = f.read()
print filename + " Content : "
print(t)
First, you should find the path of each file using os.path.join(path, filename). Otherwise you'll loop wrong files if you change the variable path. Second, your script already provides the contents of all files starting with the first one. I added a few lines to the script to print the file path and an empty line to see where the contents end and begin:
import os
path = "/Users/Desktop/test/"
for filename in os.listdir(path):
filepath = os.path.join(path, filename)
with open(filepath, 'rU') as f:
content = f.read()
print(filepath)
print(content)
print()
os.listdir returns the name of the files only. you need to os.path.join that name with the path the files live in - otherwise python will look for them in your current working directory (os.getcwd()) and if that happens not to be the same as path python will not find the files:
import os
path = "/Users/Desktop/test/"
for filename in os.listdir(path):
print(filename)
file_path = os.path.join(path, filename)
print(file_path)
..
if you have pathlib at your disposal you can also:
from pathlib import Path
path = "/Users/Desktop/test/"
p = Path(path)
for file in p.iterdir():
if not file.is_file():
continue
print(file)
print(file.read_text())

How to Save file names and their directories path in a text file using Python

I am trying to find a string that is contained in files under a directory. Then make it to store it's file names and directories under a new text file or something.
I got upto where it is going through a directory and finding a string, then printing a result. But not sure of the next step.
Please help, I'm completely new to coding and python.
import glob, os
#Open a source as a file and assign it as source
source = open('target.txt').read()
filedirectories = []
#locating the source file and printing the directories.
os.chdir("/Users/a1003584/desktop")
for root, dirs, files in os.walk(".", topdown=True):
for name in files:
print(os.path.join(root, name))
if source in open(os.path.join(root, name)).read():
print 'treasure found.'
Don't do a string comparison if your looking for a dictionary. Instead use the json module. Like this.
import json
import os
filesFound = []
def searchDir(dirName):
for name in os.listdir(dirName):
# If it is a file.
if os.isfile(dirName+name):
try:
fileCon = json.load(dirName+name)
except:
print("None json file.")
if "KeySearchedFor" in fileCon:
filesFound.append(dirName+name)
# If it is a directory.
else:
searchDir(dirName+name+'/')
# Change this to the directory your looking in.
searchDir("~/Desktop")
open("~/Desktop/OutFile.txt",'w').write(filesFound)
This should write the output to a csv file
import csv
import os
with open('target.txt') as infile: source = infile.read()
with open("output.csv", 'w') as fout:
outfile = csv.writer(fout)
outfile.writerow("Directory FileName FilePath".split())
for root, dirnames, fnames in os.walk("/Users/a1003584/desktop", topdown=True):
for fname in fnames:
with open(os.path.join(root, fname)) as infile:
if source not in infile.read(): continue
outfile.writerow(root, fname, os.path.join(root, fname))

Categories