moving a selected number of images in folder to another folder [duplicate] - python

This question already has answers here:
Select 50 items from list at random
(5 answers)
Closed 15 days ago.
i have 1200 images in source_folder, i want to copy only 500 images randomly and put them in the destination folder.
all the images names are in the list:
List = ['1.jpg, '2.jpg', '3.jpg', ...., '1200.jpg']
import glob, random
file_path_type = ["./source_folder/*.jpg"]
images = glob.glob(random.choice(file_path_type))
random_image = random.choice(images)

Use "glob" to get the whole list. Shuffle that list, and pick off the first 500. Note that random.shuffle shuffles in place.
import os, glob, random
files = glob.glob("./source_folder/*.jpg")
random.shuffle(files)
for name in files[:500]:
os.rename( name, "destination_folder")

Related

select and filtered files in directory with enumerating in loop

I have a folder that contains many eof extension files name I want to sort them in ordinary way with python code (as you can see in my example the name of all my files contain a date like:20190729_20190731 and they are just satellite orbital information files, then select and filtered 1th,24th,47th and.... (index ) of files and delete others because I need every 24 days information files( for example:V20190822T225942_20190824T005942) not all days information .for facility I select and chose these information files from first day I need so the first file is found then I should select 24 days after from first then 47 from first or 24 days after second file and so on. I exactly need to keep my desire files as I said and delete other files in my EOF source folder my desire files are like these
S1A_OPER_AUX_POEORB_OPOD_20190819T120911_V20190729T225942_20190731T005942.EOF
S1A_OPER_AUX_POEORB_OPOD_20190912T120638_V20190822T225942_20190824T005942.EOF
.
.
.
Mr Zach Young wrote this code below and I appreciate him so much I never thought some body would help me. I think I'm very close to the goal
the error is
error is print(f'Keeping {eof_file}') I changed syntax but the same error: print(f"Keeping {eof_file}")
enter code here
from importlib.metadata import files
import pprint
items = os.listdir("C:/Users/m/Desktop/EOF")
eof_files = []
for item in items:
# make sure case of item and '.eof' match
if item.lower().endswith('.eof'):
eof_files.append(item)
eof_files.sort(key=lambda fname : fname.split('_')[5])
print('All EOF files, sorted')
pprint.pprint(eof_files)
print('\nKeeping:')
files_to_delete = []
count = 0
offset = 2
for eof_file in eof_files:
if count == offset:
print(f"Keeping: [eof_file]")
# reset count
count = 0
continue
files_to_delete.append(eof_file)
count += 1
print('\nRemoving:')
for f_delete in files_to_delete:
print(f'Removing: [f_delete]')
staticmethod
Here's a top-to-bottom demonstration.
I recommend that you:
Run that script as-is and make sure your print statements match mine
Swap in your item = os.listdir(...), and see that your files are properly sorted
Play with the offset variable and make sure you can control what should be kept and what should be deleted; notice that an offset of 2 keeps every third file because count starts at 0
You might need to play around and experiment to make sure you're happy before moving to the final step:
Finally, swap in your os.remove(f_delete)
#!/usr/bin/env python3
from importlib.metadata import files
import pprint
items = [
'foo_bar_baz_bak_bam_20190819T120907_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120901_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120905_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120902_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120903_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120904_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120906_V2..._SomeOtherDate.EOF',
'bogus.txt'
]
eof_files = []
for item in items:
# make sure case of item and '.eof' match
if item.lower().endswith('.eof'):
eof_files.append(item)
eof_files.sort(key=lambda fname : fname.split('_')[5])
print('All EOF files, sorted')
pprint.pprint(eof_files)
print('\nKeeping:')
files_to_delete = []
count = 0
offset = 2
for eof_file in eof_files:
if count == offset:
print(f'Keeping {eof_file}')
# reset count
count = 0
continue
files_to_delete.append(eof_file)
count += 1
print('\nRemoving:')
for f_delete in files_to_delete:
print(f'Removing {f_delete}')
When I run that, I get:
All EOF files, sorted
['foo_bar_baz_bak_bam_20190819T120901_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120902_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120903_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120904_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120905_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120906_V2..._SomeOtherDate.EOF',
'foo_bar_baz_bak_bam_20190819T120907_V2..._SomeOtherDate.EOF']
Keeping:
Keeping foo_bar_baz_bak_bam_20190819T120903_V2..._SomeOtherDate.EOF
Keeping foo_bar_baz_bak_bam_20190819T120906_V2..._SomeOtherDate.EOF
Removing:
Removing foo_bar_baz_bak_bam_20190819T120901_V2..._SomeOtherDate.EOF
Removing foo_bar_baz_bak_bam_20190819T120902_V2..._SomeOtherDate.EOF
Removing foo_bar_baz_bak_bam_20190819T120904_V2..._SomeOtherDate.EOF
Removing foo_bar_baz_bak_bam_20190819T120905_V2..._SomeOtherDate.EOF
Removing foo_bar_baz_bak_bam_20190819T120907_V2..._SomeOtherDate.EOF

How Can You Put Input from a User into a list? [duplicate]

This question already has answers here:
How do you add input from user into list in Python [closed]
(2 answers)
Closed 3 years ago.
I've been trying to make a song shuffler but i can't put input from the user into a list. I've tried to put the input into the list but it won't work. here is the code. What should i try?
songs = list
input1 = input('type in the name of a song file you have downloaded')
songs = songs + input1
Give this a try:
songs = list()
inpt = input('type in the name of a song file you have downloaded')
songs.append(inpt)
list is a function, so it needs brackets to initialise songs as []. You append to a list by using the append() function, rather than using the addition operator. You could also use songs = songs + [inpt]
You should first go through Python basics as this is typical list operation.
songs = []
while(True):
input1 = input('type in the name of a song file you have downloaded')
songs = songs.append(input1)

For loop cycle order

I am creating a short script which tweets automatically via twitter API. Besides setting up the API credentials (out of the scope for the question) I import the following library:
import os
I have set my working directory to be a folder where I have 3 photos. If I run os.listdir('.') I get the following list.
['Image_1.PNG',
'Image_2.PNG',
'Image_3.jpg',]
"mylist" is a list of strings, practically 3 tweets.
The code that posts in Twitter automatically looks like that:
for image in os.listdir('.'):
for num in range(len(mylist)):
api.update_with_media(image, mylist[num])
The code basically assigns to the first image a tweet and posts. Then to the same image the second tweet and posts. Again first image - third tweet. Then it continues the cycle to second and third image altogether 3*3 9 times/posts.
However what I want to achieve is to take the first image with the first tweet and post. Then take second image with second tweet and post. Third image - third tweet. Then I want to run the cycle one more time: 1st image - 1st tweet, 2nd image - 2nd tweet ...etc.
Use zip to iterate through two (or more) collections in parallel
for tweet, image in zip(mylist, os.listdir('.')):
api.update_with_media(image, tweet)
To repeat it more times, you can put this cycle inside another for
Assuming the length of os.listdir('.') and mylist are equal:
length = len(mylist) # If len(os.listdir('.')) is greater than len(mylist),
# replace mylist with os.listdir('.')
imageList = os.listdir('.')
iterations = 2 # The number of time you want this to run
for i in range(0,iterations):
for x in range(0, length):
api.update_with_media(imageList[x], mylist[num])

how to handle iteratons to list different item each time

I'm trying to write a program that finds the number of different items counting the numbers of folders. And the number of each item counting the number of photos inside of each of this folders.
Once I have that I would like to make a loop that uses a different item for every iteration. For example if I have 3 different items I would like to list the first item from the first type, then the second and so on. Like this:
y=0
num=[]
for dir in next(os.walk('.'))[1]:
for folder in os.listdir(dir):
if folder.startswith("photos"):
num.append(0)
for file in os.listdir(dir+"\\"+folder):
if file.endswith(".jpg"):
num[y]+=1
y +=1
for every item since there are no more items (everyone has a diferent number)
do
for dir in next(os.walk('.'))[1]:
for folder in os.listdir(dir):
if folder.startswith("photos"):
num.append(0)
for file in os.listdir(dir+"\\"+folder):
if file.endswith(".jpg"):
What would be the correct way?
I've come with this for now, but not what I want as you can see:
y=0
num=[]
for dir in next(os.walk('.'))[1]:
for folder in os.listdir(dir):
if folder.startswith("photos"):
num.append(0)
for file in os.listdir(dir+"\\"+folder):
if file.endswith(".jpg"):
num[y]+=1
y +=1
print (num)
print (len(num))
for dir in next(os.walk('.'))[1]:
for folder in os.listdir(dir):
if folder.startswith("photos"):
for file in os.listdir(dir+"\\"+folder):
if file.endswith(".jpg"):
this could be and example of the tree directory
flowers (would be item 1)
-photos
photo1
photo2
-otherthing
books (would be item 2)
-photos
photo1
otherthing (no item because has no photos folder inside)
oterthing (no item because has no photos folder inside)
-videos

Download numbered image files in numbered folders from internet using python

The Problem
http://www.fdci.org/imagelibrary/EventCollection/1980/Big/IMG_2524.jpg
I have a link of this type where 1980 is initial folder that's changing and secondly the image filenames in format IMG_2524.jpg are changing.
what i wish to do is download all images from these url by iterating and changing these numbers within a range of 1900-2000 in case of folder and IMG_2000.jpg to IMG_4000.jpg in case of filename.
The downloaded files must be saved inside the folder number it comes from.
I think for loop should be the option but being a newbie i am somewhat lost.
please help thanks.
UPDATE
text_file = open('Output.txt', 'w')
for i in xrange(1900,2001):
for j in xrange(2000, 4001):
year = str(i)
image = str(j)
new_link = 'http://www.fdci.org/imagelibrary/EventCollection/'+year+'/Big/IMG_'+image+'.jpg'
text_file.write(new_link)
text_file.close()
thanks to anmol
Actually you need two for loops, nested for loops So now we have all 2000 - 4000 images for a given year in range 1900 - 2001
for i in xrange(1900,2001):
for j in xrange(2000, 4001):
year = str(i)
image = str(j)
new_link = 'http://www.fdci.org/imagelibrary/EventCollection/'+year+'/Big/IMG_'+image+'.jpg'
print new_link
#Now you will get the possible links within the given ranges,
#then you can use urllib2 to fetch the response from the link
# and do whatever you wanna do
Sample output:
http://www.fdci.org/imagelibrary/EventCollection/2000/Big/IMG_3994.jpg
http://www.fdci.org/imagelibrary/EventCollection/2000/Big/IMG_3995.jpg
http://www.fdci.org/imagelibrary/EventCollection/2000/Big/IMG_3996.jpg
http://www.fdci.org/imagelibrary/EventCollection/2000/Big/IMG_3997.jpg
http://www.fdci.org/imagelibrary/EventCollection/2000/Big/IMG_3998.jpg
http://www.fdci.org/imagelibrary/EventCollection/2000/Big/IMG_3999.jpg
http://www.fdci.org/imagelibrary/EventCollection/2000/Big/IMG_4000.jpg

Categories

Resources