Reputation: 213
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?
Upvotes: 0
Views: 376
Reputation: 17156
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)
Upvotes: 1
Reputation: 196
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
Upvotes: 0
Reputation: 514
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 !
Upvotes: 2