I'm trying to move specific files(only ending with *.txt) from folders under /home/wiet/import/ directory(recursive) search. But I want to do it based on the dictionary-so for example in folder /home/wiet/import/OS only files with ending *windows.txt, or *rhel.txt should be moved-about other files I want information in console about not matching files(and preferably add them to some list). For now I've got this one below, but I cannot match it with dictionary and I've got problem with copying correct files to another directory
import sys
import os
import shutil
import glob
import fnmatch
tech = {"OS": ["rhel.txt", "windows.txt"]
"DB" : ["mysql.txt","oracle.txt","postgres.txt","postgresql.txt"],
"WEB" : ["iis.txt" , "apache.txt", "tomcat.txt" ] ,
}
fileslist = []
txtcorrect = []
destin = "/home/wiet/import"
print "part1 \n \n \n"
path = os.path.join(destin)
for path, subdirs, files in os.walk(destin):
for name in files:
if name.endswith('.txt'):
print "found: " + name
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, '*.txt'):
txtcorrect.replace(os.path.join(root, filename))
print txtcorrect
shutil.copy2( txtcorrect , '/home/wiet/out')
I'm not sure what you want.
Hope this helps:
....
....
for key in tech:
if name in tech[key]:
'''do something'''
....
....
Related
i need help to find a file inside a folder by name, i can do this with one file name, how could i do this with two file name?
This is the code used
path = r"Z:/Equities/ReferencePrice/"
files = []
for file in glob.glob(os.path.join(path ,"*OptionOnEquitiesReferencePriceFile*"+"*.txt*")):
df = pd.read_csv(file, delimiter = ';')
the first file contains the name
"OptionOnEquitiesReferencePriceFile"
the Second file contains the name
"BDRReferencePrice"
how to place the second file how to search between one or the other or both
I dont think you can do that in a straightforward way, so here's an alternative solution (with a function) that you can use :
import os
from fnmatch import fnmatch
# folder path :
# here in this path i have many files some start with 'other'
# some with 'test and some with random names.
# in the example im fetchinf only the 'test' and 'other' patterns
dir_path = './test_dir'
def find_by_patterns(patterns, path):
results = []
# check for any matches and save them in the results list
for root, dirs, files in os.walk(path):
for name in files:
if max([fnmatch(name, pattern) for pattern in patterns]):
results.append(os.path.join(root, name))
return results
# printing the results
print(find_by_patterns(['test*.txt', 'other*.txt'], dir_path))
output:
['./test_dir/other1.txt', './test_dir/other2.txt', './test_dir/test1.txt', './test_dir/test2.txt', './test_dir/test3.txt']
I´ve a .txt file which has the following structure inside it:
2 PNEUMONIA/person888_bacteria_2812.jpeg
2 PNEUMONIA/person1209_bacteria_3161.jpeg
2 PNEUMONIA/person1718_bacteria_4540.jpeg
2 PNEUMONIA/person549_bacteria_2303.jpeg
2 PNEUMONIA/person831_bacteria_2742.jpeg
2 PNEUMONIA/person1571_bacteria_4108.jpeg
1 COVID-19/4-x-day1.jpg
0 HEALTHY/IM-0486-0001.jpeg
Furthermore, there are three folders:
Covid: Contains images of lungs with coronavirus
Pneumonia: Contains images of lungs with pneumonia
Healthy: Contains images with healthy lungs.
I would need to create a folder containing the images specified in this .txt file. Therefore, my question is how can I read the .txt file and move the images from these folders to a new one?
To read the .txt file :
file = open("path to the file", "r") #r is for read
mylist = file.readlines() #each line will be a new element of mylist
file.close() #don't forget to close it
So now if you do print(mylist) you get :
["2 PNEUMONIA/person888_bacteria_2812.jpeg", "2 PNEUMONIA/person1209_bacteria_3161.jpeg", "2 PNEUMONIA/person1718_bacteria_4540.jpeg", "2 PNEUMONIA/person549_bacteria_2303.jpeg", "2 PNEUMONIA/person831_bacteria_2742.jpeg", "2 PNEUMONIA/person1571_bacteria_4108.jpeg", "1 COVID-19/4-x-day1.jpg", "0 HEALTHY/IM-0486-0001.jpeg"]
So you can loop over all the elements with a for loop... Then how to create the folder and the files ?
To create the folder :
import os
dirName = 'your_path'
try:
os.mkdir(dirName)
print("Directory " , dirName , " Created ")
except FileExistsError:
print("Directory " , dirName , " already exists")
This will create a folder, but won't do it if it already exists.
Then, move the files :
import shutil, os
for f in mylist:
shutil.move(f, dirName)
With dirName in the precedent code.
So the full code looks like :
import shutil, os
file = open("path to the file", "r")
mylist = file.readlines()
file.close()
dirName = 'your_path'
try:
os.mkdir(dirName)
print("Directory " , dirName , " Created ")
for f in mylist:
shutil.move(f, dirName)
except FileExistsError:
print("Directory " , dirName , " already exists")
But, if we consider your file :
2 PNEUMONIA/person888_bacteria_2812.jpeg
2 PNEUMONIA/person1209_bacteria_3161.jpeg
2 PNEUMONIA/person1718_bacteria_4540.jpeg
2 PNEUMONIA/person549_bacteria_2303.jpeg
2 PNEUMONIA/person831_bacteria_2742.jpeg
2 PNEUMONIA/person1571_bacteria_4108.jpeg
1 COVID-19/4-x-day1.jpg
0 HEALTHY/IM-0486-0001.jpeg
Maybe the first characters, those numbers, aren't desired in the name of the file ?
Then just search for split method and have fun !
Warning about moving the files :
When we do :
for f in mylist:
shutil.move(f, dirName)
We are assuming that the script is in the same location as the pictures, so it takes just f as the path and it is good. But if it is located anywhere else, you should do something like :
for f in mylist:
shutil.move("path_to_the_original_folder_+_an_\_at_the_end"+f, dirName)
For exemple :
for f in mylist:
shutil.move("C:\Covid\"+f, dirName)
There it is !
Code
# source folder such as '.' or os.getcwd(), etc.
src ='source path'
# desitination folder such as '.' or os.getcwd(), etc.
dst = 'destination folder'
with open('file.txt') as file:
for line in file:
# Get parent folder and filename
# such as PNEUMONIA/person888_bacteria_2812.jpeg"
relative_path = line.rstrip().split(' ', 1)[1].strip()
# Full source path (prepend src to relative path)
src_path = os.path.join(src, relative_path)
# Destination folder
destination = os.path.join(dst, path)
# make directory if it does not exists
# Path(destination).parent is parent folder of
# destination
os.makedirs(Path(destination).parent, exist_ok = True)
# Move file
shutil.move(src_path, destination)
use this code:
import shutil
new_path = (r"C:\your\new\path")
file = open(r"C:\your\list\path\list.txt","r")
read_lines= file.readlines()
file.close()
for path in read_lines:
shutil.copy(path.replace("\n",""),new_path)
make sure the paths look like this in your list:
C:\like\this\path\filename.jpeg
C:\like\this\path\filename.jpeg
C:\like\this\path\filename.jpeg
C:\like\this\path\filename.jpeg
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)
I have a working script that will print all files in a given directory. I would like help making it do two additional things:
(1) Also be able to print the date_created or time stamp for each file.
(2) Do all of the above not only for files in the given directory, but in all subdirectories as well.
Here is the working script:
from os import listdir
from os.path import isfile, join
from sys import argv
script, filename = argv
mypath = os.getcwd()
allfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
output = open(filename, 'w')
for i in allfiles:
string = "%s" %i
output.write(string + "\n")
output.close()
print "Directory printed."
I would hope to be able to print something like (filename + ", " + timestamp + "\n"), or some substitute.
Thanks!
http://docs.python.org/2/library/os.html and http://docs.python.org/2/library/stat.html have you covered.
os.walk will give you the recursive directory walking
stat will give you file timestamps (atime,ctime,mtime)
This snippet walks through files in a directory + subdirectories and prints out created and modified timestamps.
import os
import time
def walk_files(directory_path):
# Walk through files in directory_path, including subdirectories
for root, _, filenames in os.walk(directory_path):
for filename in filenames:
file_path = root + '/' + filename
created = os.path.getctime(file_path)
modified = os.path.getmtime(file_path)
# Process stuff for the file here, for example...
print "File: %s" % file_path
print " Created: %s" % time.ctime(created)
print " Last modified: %s" % time.ctime(modified)
walk_files('/path/to/directory/')
So I'm trying to iterate through a list of files that are within a subfolder named eachjpgfile and change the file from doc to the subfolder eachjpgfile mantaining the file's name but when I do this it adds the file to directory before eachjpgfile rather than keeping it in it. Looking at the code below, can you see why is it doing this and how can I keep it in the eachjpgfile directory?
Here is the code:
for eachjpgfile in filelist:
os.chdir(eachjpgfile)
newdirectorypath = os.curdir
list_of_files = os.listdir(newdirectorypath)
for eachfile in list_of_files:
onlyfilename = os.path.splitext(eachfile)[0]
if onlyfilename == 'doc':
newjpgfilename = eachfile.replace(onlyfilename,eachjpgfile)
os.rename(eachfile, newjpgfilename)
There is a lot of weird stuff going on in here, but I think the one that's causing your particular issue is using 'eachjpgfile' in 'eachfile.replace'.
From what I can tell, the 'eachjpgfile' you're passing in is a full-path, so you're replacing 'doc' in the filename with '/full/path/to/eachjpgfile', which puts it parallel to the 'eachjpgfile' directory regardless of your current working directory.
You could add a line to split the path/file names prior to the replace:
for eachjpgfile in filelist:
os.chdir(eachjpgfile)
newdirectorypath = os.curdir
list_of_files = os.listdir(newdirectorypath)
for eachfile in list_of_files:
onlyfilename = os.path.splitext(eachfile)[0]
if onlyfilename == 'doc':
root, pathName= os.path.split(eachjpgfile) #split out dir name
newjpgfilename = eachfile.replace(onlyfilename,pathName)
os.rename(eachfile, newjpgfilename)
which is a very dirty fix for a very dirty script. :)
try this:
import os
path = '.'
recursive = False # do not descent into subdirs
for root,dirs,files in os.walk( path ) :
for name in files :
new_name = name.replace( 'aaa', 'bbb' )
if name != new_name :
print name, "->", new_name
os.rename( os.path.join( root, name),
os.path.join( root, new_name ) )
if not recursive :
break