I'm trying to find and replace texts in all sub directorys of the given directory and here is my code.
import os
folderLocation = "C:\\Users\\josh\\Desktop\\target\\xml\\"
divider = "\\"
pseudoDictionaryKey = []
pseudoDictionaryValue = []
with open("C:/Users/josh/Desktop/FILENAMES.txt", "r") as fi:
for line in fi:
tokens = line.strip().split("-->")
pseudoDictionaryKey.append(tokens[0])
pseudoDictionaryValue.append(tokens[1])
for subdir, dirs, files in os.walk(folderLocation):
for fileIn in files:
if fileIn in pseudoDictionaryKey:
os.rename(folderLocation + subdir, folderLocation + pseudoDictionaryValue[pseudoDictionaryKey.index(fileIn)])
for subdir, dirs, files in os.walk(folderLocation):
for fileIn in files:
result = ""
with open(folderLocation + fileIn,"r") as fi:
result = fi.read()
for i, value in enumerate(pseudoDictionaryKey):
result = result.replace(pseudoDictionaryKey[i],pseudoDictionaryValue[i])
with open(folderLocation + fileIn,"w") as fo:
fo.write(result)
It finds and replaces the texts but it does not knows how to enter subdirs I want it to rename the texts in every files inside the directory and sub directorys like Open every file inside the directory not just the ones defined in FILENAMES.txt
Related
I have the following files in txt format:
Expected File Format: I want to remove prefix from file name that is 1. a1. and while renaming if the file already present with same name then append _1, _2 to the file as given below in example.
My try:
import os
import re
import shutil
import argparse
pattern = "a1"
path = "/Users/a1/Documents/Files"
count = 0
p = ".* "+str(pattern)+".(.+)"
for root, dirs, files in os.walk(path):
for file in files:
m = re.match(p, file)
if m is not None:
file_new = m.group(1)
if not os.path.exists(os.path.join(root,file_new)):
os.rename(os.path.join(root, file), os.path.join(root,file_new))
else:
count = count + 1
file_new = m.group(1)+"_"+str(count)
os.rename(os.path.join(root, file), os.path.join(root,file_new))
And this is what the output I'm getting:
You can use Dict for saving the count of repeating each file_name and use saving count in Dict for renaming.
import os
import re
pattern = "a1"
path = "Files/"
dct = {} # <- adding this
for root, dirs, files in os.walk(path):
for file in files:
if pattern in file:
file_new = file.split(pattern, 1)[1]
if not file_new in dct: # <- adding this
os.rename(os.path.join(root, file),
os.path.join(root,file_new[1:]))
dct[file_new] = 1 # <- adding this
else:
num = dct[file_new] # <- adding this
dct[file_new] += 1 # <- adding this
file_name, file_type = file_new[1:].split('.')
os.rename(os.path.join(root, file),
os.path.join(root, f'{file_name}_{num}.{file_type}'))
Filename before renaming:
Filename after renaming:
I have some textfiles in a folder and I want to search all of them for names between row 4 and 20 and then copy the ones containing one of those names to a different folder. With my code I only get an empty result file even though I know the keywords are in my folder. What could be the problem with this code for Python 3?
from os import system, listdir, path
import codecs
FILE = open('C:\\Users\\Admin\\Desktop\\Test\\Result.txt', 'w')
desktop_dir = path.join('C:\\Users\\Admin\\Desktop\\test\\')
for fn in listdir(desktop_dir):
fn_w_path = path.join(desktop_dir, fn)
if path.isfile(fn_w_path):
with open(fn_w_path, "r") as filee:
for line in filee.readlines():
for word in line.lower().split():
if word in {'James',
'Tim',
'Tom',
'Ian',
'William',
'Dennis',}:
FILE.write(word + "\n")
FILE.close()
import os
import shutil
for root, dirs, files in os.walk("test_dir1", topdown=False):
for name in files:
current_file = os.path.join(root, name)
destination = current_file.replace("test_dir1", "test_dir2")
print("Found file: %s" % current_file)
print("File copy to: %s" % destination)
shutil.copy(current_file, destination)
There are text files of various names in the folder 'a'. I want to read all of these text files and add the letter 'b' to each text file. What should I do?
cwd = os.getcwd()
input_dir = os.path.join(cwd, "my .txt files dir")
sorts = sorted(glob(input_dir), key = lambda x:(len(x) , x))
for f in sorts :
f = open(input_dir, 'a')
data = "add text"
f.write(data)
f.close()
Append data to file:
- first: get all file in folder a.
- second: find extension with .txt.
- third: open it and do something('append', or 'rewrite').
Demo:
import os
# your .txt files dir
path = 'a'
# append data what you want
appendData = 'b'
fileNames = list(os.walk(path))[0][2]
fileNames.sort(key=len)
fileNums = len(fileNames)
# your dst file extension
fileExt = '.txt'
# # Extract extension from filename
# fileExt = os.path.splitext(fileNames[0])[1]
for fileName in fileNames:
if fileName.endswith(fileExt):
fileFullPath = os.path.join(path, fileName)
with open(fileFullPath, 'a') as f:
f.write(appendData)
Like the others said, this is an easy question that could easily be find on google. Anyway here's how to do it:
from os import listdir
from os.path import isfile, isdir, join
files = [file for file in listdir("files") if isfile(join("files", file))]
directories = [directory for directory in listdir("files") if isdir(join("files", directory))]
print(files)
for file_name in files:
try:
file = open("files/" + file_name, "a")
file.write("b")
file.close()
except IOError as err:
print("Could not open file because : ", err)
Replace "file" with the directory where your files are or the path to that directory like "directory0/directory1/directory_with_files"
Avoid to open files with
f = open(input_dir, 'a')
f.close()
Instead
with open(input_dir, 'a') as inputFile:
Do something
Also what you want is
import os
import glob # We will use this module to open only .txt files
path = 'your/path'
for filename in glob.glob(os.path.join(path, '*.txt'))
with open(filename, 'a') as inputFile:
inputFile.write('b')
Guys I was trying to search for a word that will be entered by the user in a list of files in a folder and so far I have this code :
import os
folderpath = "C:\\Users\\user\\Desktop\\Documents"
word = input("Choose a word : ")
for(path, dirs, files) in os.walk(folderpath, topdown=True):
for filename in files:
filepath = os.path.join(path, filename)
with open(filepath, 'r') as f:
info = f.readlines()
for line in info:
if word in line:
print( filename + ":" + "[1]" )
else:
print(filename + "[0]")
the output is the name of each file 10 times then 1,0,1,0... respectively(ex: Doc1[1] , Doc1[0] , Doc1[1]....). It looks like nothing is breaking the loop. Please any help
Your code is printing output for every line in every file, not just 10 times. I suspect your files are all 10 lines long if that is the case.
The following code just tests str(info) for the word, printing one match for each file:
import os
folderpath = "C:\\Users\\user\\Desktop\\Documents"
word = input("Choose a word : ")
for(path, dirs, files) in os.walk(folderpath, topdown=True):
for filename in files:
matched = 0
filepath = os.path.join(path, filename)
with open(filepath, 'r') as f:
info = f.readlines()
if word in str(info):
matched = 1
print("{}: [{}]".format(filename, matched))
If you wish your test to be case-insensitive, simply replace:
if word in str(info):
With:
if word.casefold() in str(info):
If you'd like to have an actual count of occurrences for each file in your output, you can do something like:
import os
folderpath = "C:\\Users\\user\\Desktop\\Documents"
word = input("Choose a word : ")
for(path, dirs, files) in os.walk(folderpath, topdown=True):
for filename in files:
count = 0
filepath = os.path.join(path, filename)
with open(filepath, 'r') as f:
info = f.readlines()
if word in str(info):
for line in info:
if word in line:
count += 1
print("{}: [{}]".format(filename, str(count)))
Take a look at the modified line 13 below, and you'll see each match along with the filename.
import os
folderpath = "C:\\Users\\user\\Desktop\\Documents"
word = input("Choose a word : ")
for(path, dirs, files) in os.walk(folderpath, topdown=True):
for filename in files:
filepath = os.path.join(path, filename)
with open(filepath, 'r') as f:
info = f.readlines()
for line in info:
if word in line:
print( filename + ":" + "[1]:", line )
else:
print(filename + "[0]")
Suppose I have a text file aiq_hits.txt.
Each line in this file corresponds a filename
ant1.aiq
ant2.aiq
ant3.aiq
ant4.aiq
I want to match each line of my textfile (ant1.aiq,ant2.aiq and so on) with filenames which are present at some specific place(R:\Sample) and extract matching files into some other place (R:\sample\wsa).
I have an idea that I need to use functions like os.walk() and fnmatch.fnmatch(), shutil.copy() but I am not able to implement them
My code:
import os
import shutil
import fnmatch
with open("aiq_hits.txt","r") as in_file:
for line in in_file:
I am stuck here
import os
import shutil
sourceDir = "R:\\Sample"
targetDir = "R:\\Sample\\wsa"
existingFiles = set(f for f in os.listdir(sourceDir) if os.path.isfile(os.path.join(sourceDir, f)))
infilepath = "aiq_hits.txt"
with open(infilepath) as infile:
for line in infile:
fname = line.strip()
if fname not in existingFiles: continue
shutil.move(os.path.join(sourceDir, fname), os.path.join(targetDir, fname))
I hope this will suffice:
import os
def match_files(url,file_read, dest):
f = open(file_read, 'rb')
file_list = os.listdir(url)
print(file_list)
saved_path = os.getcwd()
print("Current working directory is " + saved_path)
os.chdir(url)
match = []
for file_name in f:
file_name = file_name.strip()
if file_name in file_list:
match.append(file_name)
os.rename(os.path.join(url, file_name), os.path.join(dest, file_name))
os.chdir(saved_path)
print match
here, url is source directory or folder from which u want to match files, file_read is the name of file (with path) in which list of file names is given, dest is the destination folder.
this code moves the matching files from url to dest, i.e. these files won't remin in url after running the code.
Alternatively you could use the glob module which allows you to enter in a expression for the file name\extension which will then return a list that you can loop over.
I'd use this module if the source directory can have files with the same extension that you want to exclude from being looped over
Also I'm assuming that the file name list is not large and so storing it in a list wont be an issue
eg (I haven't tested the below )
from glob import glob
import os
import shutil
src = 'R:\\Sample'
dst = "R:\\Sample\\wsa"
in_file_list = "aiq_hits.txt"
list_Of_files = glob(os.path.join(src, 'ant*.aiq'))
data = []
with open(in_file_list) as reader:
data += reader.readlines()
for row in list_Of_files:
file_path, file_name = os.path.split(row)
if file_name in data:
shutil.copy2(row, os.path.join(dst, file_name))
# or if you want to move the file
# shutil.move(row, os.path.join(dst, file_name))