I have written a code for moving files from one folder to another .I moved only those files whose names are present in my text file(aiq.txt).
It works fine when files are less in number say(10) but when number goes to around 500 the program terminates.
Each file is of 50 Mb
Here is my code:
import os
import shutil
destination=r"dstpath"
with open("aiq.txt") as infile:
for line in infile:
line=line.strip('\n)
for root,dirs,filenames in os.walk(r"H:\test_vectors"):
for filename in filenames:
if line in filename:
shutil.move(os.path.join(root,filename),destination)
import os
import shutil
root = r"H:\test_vectors"
destination = r"dstpath"
file_list = []
dir_dict = {}
with open('aiq.txt', 'r') as f:
for line in f:
file_list.append(line.strip())
for d, dirs, files in os.walk(root):
if files:
dir_dict[d] = files
for key, item in dir_dict.items():
for x in item:
if x in file_list:
try:
shutil.move(os.path.join(key, x), destination)
except:
pass
Related
Need a bit of help putting this together
This script reads any files with or with out an .ext
this can also edit the original file
I would like to isolate it to a file name
because it also reads and edits my other scripts
and it edited itself
import os
for root, dirs, files in os.walk('.'):
for name in files:
nmin = os.path.join(root,name)
with open(nmin,"r") as fin:
data = fin.read()
data = data.replace('"Houston": ??,",', '"Houston": City",') \
.replace('"Place": USA,",', '"Place": County",')
with open(nmin,"w") as fout:
fout.write(data)
I'm trying different ways and can't get it to work right
import os
for root, dirs, files in os.walk('CityPlaces.txt'):
for name in files:
nmin = os.path.join(root,name)
with open(nmin,"r") as fin:
data = fin.read()
data["Houston"] = City
data["Place"] = Country
with open(nmin,"w") as fout:
fout.write(data)
Also I'm trying to make data [ ] work, because every txt file I work on will have different content
For example:
"Houston": Random data,
"Houston": Random data,
"Houston": Random data,
but all will be update with the same write
so replace will not work
I want to change this
data = data.replace('"Houston": ??,",', '"Houston: City",') \
.replace('"Place": USA,",', '"Place: County",')
to this
data = fin.read()
data["Houston"] = City
data["Place"] = Country
Any help would be great
I got help with this part Thank you very much
for root, dirs, files in os.walk('.'):
for name in files:
if name != "CityPlaces.txt": continue
How do I add this to my script
>>> import re
>>> p = re.compile('ab*')
>>> p
re.compile('ab*')
I'm guessing like this
import os
import re
for root, dirs, files in os.walk('CityPlaces.txt'):
for name in files:
nmin = os.path.join(root,name)
with open(nmin,"r") as fin:
data = fin.read()
data = re.compile('Houston*')
data = re.compile('Place*')
with open(nmin,"w") as fout:
fout.write(data)
So, you have files called CityPlaces.txt in multiple directories? Then, just check that your directory contains such a file, and process only that file:
import os
for root, dirs, files in os.walk('.'):
if 'CityPlace.txt' not in files:
continue
nmin = os.path.join(root,'CityPlace.txt')
with open(nmin,"r") as fin:
data = fin.read()
data = data.replace('"Houston": ??,",', '"Houston": City",') \
.replace('"Place": USA,",', '"Place": County",')
with open(nmin,"w") as fout:
fout.write(data)
I want to get my script to read a list of names from a list(txt), then search for those in a selected folder with subfolders, then copy and paste those files to another selected folder. My script running without error but no result.
My script:
import os
import os.path
import shutil
textFile = ("D:\\Test\\list.txt")
sourceFolder = ("D:\\Test")
destinationFolder = ("D:\\")
filesToFind = []
with open(textFile, "r") as tx:
for row in tx:
filesToFind.append(row.strip())
for root, dirs, filename in os.walk(sourceFolder):
if filename in filesToFind:
f = os.path.join(root, filename)
shutil.copy(f, destinationFolder)
Haven’t test it but I think this will work - change this:
for root, dirs, filename in os.walk(sourceFolder):
if filename in filesToFind:
f = os.path.join(root, filename)
shutil.copy(f, destinationFolder)
To this:
for root, dirs, filenames in os.walk(sourceFolder):
for filename in filenames:
if filename in filesToFind:
f = os.path.join(root, filename)
shutil.copy(f, destinationFolder)
# Same code using glob #
## More efficient and also tested one ##
## One more feature added- checks file name given present or not ##
import os
import os.path
import shutil
import glob
textFile = ("D:\\Test\\list.txt")
sourceFolder = ("D:\Test")
destinationFolder = ("D:\\")
f = open(textFile, "r").readlines()
for i in f:
ListFile= glob.glob(os.path.join(sourceFolder,"**",i.strip()),recursive=True)
if len(ListFile):
print(ListFile[0],destinationFolder,os.path.basename(ListFile[0]))
destinationfile=os.path.join(destinationFolder,os.path.basename(ListFile[0]))
shutil.copyfile(ListFile[0],destinationfile)
else:
print(i,"-File not found")
I'm newbie in Python,
I need to create a large quantity of files with random names in Dest_Dir (my destination directory), And then Zip them to one file.
Does anyone have an idea how to do that?
I managed to create files like that with a for loop into a specific folder, but it doesn't fit in case I want to create large amount of file (Lets say 100)
And the names I created aren't random.
import os
import sys
import platform
SRC_Dir = os.path.dirname(__file__)
Dest_Dir = os.path.join(SRC_Dir, 'dest')
items = ["one", "two", "three"]
for item in items:
#(os.path.join(Dest_Dir, filename), 'wb') as temp_file:
with open(os.path.join(Dest_Dir, item), 'wb') as f:
f.write("This is my first line of code")
f.write("\nThis is my second line of code with {} the first item in my list".format(item))
f.write("\nAnd this is my last line of code")
You could make use of the built-in tempfile
import os
import tempfile
for _ in range(100):
file_descriptor, file_path = tempfile.mkstemp(".txt", "prefix-", Dest_Dir)
file_handle = open(file_path, "wb")
# do stuff
os.close(file_descriptor)
file_handle.close()
Since a comment was made about the zip part I figured I would add that as well
import os
import tempfile
import zipfile
new_files = []
for _ in range(10):
file_descriptor, file_path = tempfile.mkstemp(".txt", "prefix-", "/tmp")
file_handle = open(file_path, "wb")
file_handle.write("HELLO")
os.close(file_descriptor)
file_handle.close()
new_files.append(file_path)
with zipfile.ZipFile("/tmp/zipped.zip", "w") as zipped:
for file_path in new_files:
zipped.write(file_path, os.path.basename(file_path))
The zipped.write arguments here assume only the filename (and not the path) are desired for the archived name.
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))
I'd like to remove the first line and the last second line of files which exits in different sub directories in the same root directory. And the codes as below
import fileinput
import sys
import os
path = "./rootDire"
for(dirpath,dirnames,files) in os.walk(path):
f = open(file,'r')
lines = f.readlines()
f.close()
f = open(file,'w')
f.writelines(lines[1:-2])
f.close()
But, when it found the file, the error happened saying no the file which has already been found.
Correct me if it does not work:
import fileinput
import sys
import os
path = "./rootDire"
for(dirpath,dirnames,files) in os.walk(path):
for filename in files:
filepath = os.path.join(dirpath, filename)
f = open(filepath,'r')
lines = f.readlines()
f.close()
f = open(filepath,'w')
f.writelines(lines[1:-2])
f.close()