How to select specific files from a .txt and create a folder - python

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

Related

Find keywords from txt files and copy target files

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)

python How do I import multiple .txt files in a folder to add characters to each .txt file?

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')

Python rename file based on file in same directory

I have a file directory structure like this:
/folder
aaa.pdf
bbb.xml
stamped.pdf
Where PDF and XML file names have no patterns to them, except that every folder has stamped.pdf in it (stamped.pdf needs to be ignored).
I want to rename the .xml file in the directory to match the .pdf file name, so I end up with:
/folder
aaa.pdf
aaa.xml
stamped.pdf
Python so far (not renaming anything yet, just trying to get the filenames at this point)
import os
pdf = ('.pdf')
xml = ('.xml')
stamped = ('stamped.pdf')
for folderName, subfolders, filenames in os.walk('folder'):
print('The current folder is ' + folderName)
for filename in filenames:
namefile = os.path.splitext(filename)[0]
if (filename.endswith(pdf) and filename != stamped):
pdfname = namefile
print('PDF File Name: ' + pdfname)
if filename.endswith(xml):
print('RENAME XML FILE NAME: ' + namefile + 'TO: ' pdfname)
else:
print('')
print('')
Right now I'm just printing values before I get into the renaming.
In the script above, pdfname is undefined in the XML conditional, because the pdfname variable isn't set/available in the XML conditional.
How can I pass the pdfname variable so that it can be used to rename the XML file in the same directory?
Thanks!
import os
for parent, _, files in os.walk('.'):
if not files:
continue
pdf_file = None
xml_file = None
for filename in files:
if filename.lower().endswith('.pdf') and filename.lower() != 'stamped.pdf':
pdf_file = filename
elif filename.lower().endswith('.xml'):
xml_file = filename
new_xml_filename = '{}/{}.xml'.format(parent, os.path.splitext(pdf_file)[0])
xml_file = '{}/{}'.format(parent, xml_file)
if os.path.exists(new_xml_filename):
print('cannot rename %s without overwriting an existing file. skipping' % xml_file)
continue
else:
os.rename(xml_file, new_xml_filename)
print('renamed {} -> {}'.format(xml_file, new_xml_filename))

Patch Movement: File Backup and Replace

I'm getting an error while trying to copy files from a single source directory which contains bug fixes: /home/saurabh/testbed/patch_dir/ to multiple destination directories: app_dir_1 and app_dir_2.
Both these directories are exact replicas of each other.
The script does the following:-
Read lines from a text file into a list. Each line contains the name of one component. In this case: ['file1.class', file2.html]
Search value at each index recursively, starting from a particular directory:
/home/saurabh/testbed/dest_dir/
Take a backup of these files wherever they are found by appending ddMonyyyy to their extension.
Copy files from directory which contains patched components: /home/saurabh/testbed/patch_dir/
to the directory where backup was taken earlier
Directory Overview:-
/home/saurabh/testbed/dest_dir/
|--app_dir_1
|--file1.class
|--file2.html
|--file3.jsp
|--file4.xml
|--sub_dir
|--app_dir_2
|--file1.class
|--file2.html
|--file3.jsp
|--file4.xml
|--sub_dir
|--other_directories
/home/saurabh/testbed/patch_dir/
|--file1.class
|--file2.html
Below is my code:
#!/usr/bin/env python
import os
import fnmatch
import datetime
import shutil
with open('filenames.txt') as f:
content = f.readlines()
content = [x.strip() for x in content]
print('File contents:')
print(content)
suffix = datetime.datetime.now().strftime("_%d%b%Y")
approot = '/home/saurabh/testbed/dest_dir/'
source_dir = '/home/saurabh/testbed/patch_dir/'
dir_list = []
print('\n' + 'Renaming files present at:')
for root, dirs, files in os.walk(approot):
for file_list in content:
for filename in fnmatch.filter(files, file_list):
print(os.path.join(root, filename))
dir_list.append(root)
current_file = os.path.join(root, filename)
backup_file = os.path.join(root, filename + suffix)
os.rename(current_file, backup_file)
print("\n" + "Backup of all files complete!")
print('Backup of ' + str(len(dir_list)) + ' files taken recursively')
print('Number of files mentioned in text file: ' + str(len(content)) + '\n')
# 2 instances in UAT
# 12 instances in PROD
if (2*len(content)) == len(dir_list):
print("Retrofitted components will be copied to their respective directories")
for dst_ind in range(0, len(dir_list)):
if filename in fnmatch.filter(files, file_list):
print(source_dir + content[dst_ind] + "\t" + dir_list[dst_ind])
#shutil.copy2(source_dir+content[dst_ind], dir_list[dst_ind])
I'm getting the below error while copying the files (4.)
File contents:
['file1.class', 'file2.html']
Renaming files present at:
/home/saurabh/testbed/dest_dir/app_dir_1/file1.class
/home/saurabh/testbed/dest_dir/app_dir_1/file2.html
/home/saurabh/testbed/dest_dir/app_dir_2/file1.class
/home/saurabh/testbed/dest_dir/app_dir_2/file2.html
Backup of all files complete!
Backup of 4 files taken recursively
Number of files mentioned in text file: 2
Retrofitted components will be copied to their respective directories
/home/saurabh/testbed/patch_dir/file1.class /home/saurabh/testbed/dest_dir/app_dir_1
/home/saurabh/testbed/patch_dir/file2.html /home/saurabh/testbed/dest_dir/app_dir_1
Traceback (most recent call last):
File "./prod_movement.py", line 56, in <module>
print(source_dir + content[dst_ind] + "\t" + dir_list[dst_ind])
IndexError: list index out of range
Expected Output:
File contents:
['file1.class', 'file2.html']
Renaming files present at:
/home/saurabh/testbed/dest_dir/app_dir_1/file1.class
/home/saurabh/testbed/dest_dir/app_dir_1/file2.html
/home/saurabh/testbed/dest_dir/app_dir_2/file1.class
/home/saurabh/testbed/dest_dir/app_dir_2/file2.html
Backup of all files complete!
Backup of 4 files taken recursively
Number of files mentioned in text file: 2
Retrofitted components will be copied to their respective directories
/home/saurabh/testbed/patch_dir/file1.class /home/saurabh/testbed/dest_dir/app_dir_1
/home/saurabh/testbed/patch_dir/file2.html /home/saurabh/testbed/dest_dir/app_dir_1
/home/saurabh/testbed/patch_dir/file1.class /home/saurabh/testbed/dest_dir/app_dir_2
/home/saurabh/testbed/patch_dir/file2.html /home/saurabh/testbed/dest_dir/app_dir_2
Appreciate any help to fix the code.
Figured it out !!
#!/usr/bin/env python
import os
import fnmatch
import datetime
import shutil
with open('filenames.txt') as f:
content = f.readlines()
content = [x.strip() for x in content]
print('File contents:')
print(content)
suffix = datetime.datetime.now().strftime("_%d%b%Y")
approot = '/Users/saurabhm/Desktop/Python/testbed/dest_dir/'
source_dir = '/Users/saurabhm/Desktop/Python/testbed/patch_dir/'
dir_list = []
print('\n' + 'Renaming files present at:')
for root, dirs, files in os.walk(approot):
for file_list in content:
for filename in fnmatch.filter(files, file_list):
print(os.path.join(root, filename))
dir_list.append(root)
current_file = os.path.join(root, filename)
backup_file = os.path.join(root, filename + suffix)
#print(current_file, backup_file)
os.rename(current_file, backup_file)
#print(source_dir + filename + "\t" + root)
shutil.copy2(source_dir + filename, root)
os.chmod(root + '/' + filename, 0o750)
print("\n" + "Backup of all files complete!")
print('Backup of ' + str(len(dir_list)) + ' files taken recursively')
print('Number of files mentioned in text file: ' + str(len(content)) + '\n')
Once patched components are received over email, they are copied to a directory in the respective server. All these files are consolidated in a single directory (patch_dir)
Backup of existing files (having the same name and are present in "dest_dir") are taken wherever they are found, following which each file is copied from "patch_dir" to the directories inside "dest_dir", where their backup was taken.

python move file based on dictionary

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'''
....
....

Categories

Resources