moving files to folders after they got read and separated by a variable
moving files to folders after they got read and separated by a variable
I have created a python script that reads a variable FILTER
from files in a folder and puts the result on screen. However, there are 4 types of variables and I would like this script to separate them all to the corresponding folders. Like move all of the files to a folder named "V" if they have variable FILTER = V
, if they have FILTER = B
, then move all of the B
ones to folder named "B" The script below works to see which files have which filter on screen.
FILTER
FILTER = V
FILTER = B
B
import glob
import pyfits
import shutil
myList =
for fitsName in glob.glob('*.fits'):
hdulist = pyfits.open(fitsName)
b = hdulist[0].header['FILTER']
c = b
myList.append(c)
hdulist.close()
for item in sorted(myList):
print item
Result on screen:
B
B
B
V
V
V
R
R
R
I
I
I
now with shutil the code i run;
import os
import glob
import pyfits
import shutil
myList =
for fitsName in glob.glob('*.fits'):
hdulist = pyfits.open(fitsName)
hdu = hdulist[0]
prihdr = hdulist[0].header
a = hdulist[0].header['FILTER']
b = a
if b == "B":
shutil.move('/home/usr/Desktop/old/', '/home/usr/Desktop/new/B/')
myList.append(b)
hdulist.close()
Now this code works without problem but it moves all the files in Desktop/old/ to Desktop/new/B/ however, some files have b = V and other variables so what is the problem here? How can I specify the names of which files have the filters I desired so that it can automatically move?
so it is like from the code above, if c= FILTERNAME1 move to SOMEFOLDER1 if c = FILTERNAME2 move to SOMEFOLDER2 and so on.. I could not write a working code line for this so any help would be appreciated a lot.
Solution;
import os
import glob
import pyfits
import shutil
for fitsName in glob.glob('*.fits'):
hdulist = pyfits.open(fitsName)
hdu = hdulist[0]
a = hdulist[0].header['FILTER']
if a == "B":
shutil.move(fitsName, '/home/usr/Desktop/new/B/')
if a == "V":
shutil.move(fitsName, '/home/usr/Desktop/new/V/')
if a == "R":
shutil.move(fitsName, '/home/usr/Desktop/new/R/')
if a == "I":
shutil.move(fitsName, '/home/usr/Desktop/new/I/')
1 Answer
1
You can use the shutil module to move files.
shutil.move(source,destination)
Define the source file and the destination files as strings, then pass them to shutil.move()
like so:
shutil.move()
import shutil
if c == "A":
shutil.move(source, destA)
elif c == "B":
shutil.move(source, destB)
I would also recommend that you learn how if statements work. Here are some resources: https://www.tutorialspoint.com/python/python_if_else.htm, https://www.w3schools.com/python/python_conditions.asp, https://docs.python.org/3/tutorial/controlflow.html
@NapolyoN I don't understand anything that you just wrote. I would like you to edit your question to include the full errors you are getting and include ALL relevant code, input, and output.
– Simon
Sep 4 '18 at 20:12
here I included the code fully. The one I use with the result and the one I need with the result.
– NapolyoN
Sep 4 '18 at 20:22
@NapolyoN You're writing the
if
statements incorrectly. They need to end with :
. Please read the links I gave you. What you have also won't work because by the time you reach the if statements c
no longer exists.– Simon
Sep 4 '18 at 20:26
if
:
c
Thank you, I finally understood them all and modified the code up there too. Here now everything works however, all files get moved regardless of what parameter introduce. I explained the final situation up there in details.
– NapolyoN
Sep 4 '18 at 21:12
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The problem with this is, when I use my print item code, I see B B B B V V V V R R R R I I I I listed downwards so when i put if c == B and shutil.move(source, dest) i get syntax error, same for c == "B" as well. My c is either B V R or I but whatever I give to c == , I get error...
– NapolyoN
Sep 4 '18 at 19:53