Check if all files are there in directory before proceeding to execute the remaining code?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Check if all files are there in directory before proceeding to execute the remaining code?



I'm trying to make sure that all the files(here in my case 'ccf_table','bis_file') are there in the directory before the code proceed to start the computations. And if either of them(bis_file,ccf_table) is not there in directory for any of the input file('filename' in my case) then just simply pass that input file.


path = "/home/Desktop/s1d_data"
for filename in os.listdir(path):
if filename.endswith("_s1d_A.fits"):
s1d_hdu = fits.open(filename)
s1d_header = s1d_hdu[0].header
date = s1d_header['DATE-OBS']
date2 = date = date[0:19]
ccf_table = glob('HARPS.' + date2 + '*_ccf_G2_A.tbl')
bis_file = glob('HARPS.' + date2 + '*_bis_G2_A.fits')

filelist = ['ccf_table','bis_file']
while True:
list1 =
for file in filelist:
list1.append(os.path.isfile(file))

if all(list1):
break
else:
pass

df=pd.read_table(ccf_table[0],skiprows=2,usecols=(0,4),names=['order','rv'],)
df = df.rv.mean()
out_name = s1d_header['OBJECT'] +'-' + s1d_header['DATE-OBS'] +'.fits'



That's what i've tried until now but didn't get the desired result.Any help would be appreciated.





Have you checked out using os.path.isfile? stackoverflow.com/questions/2259382/…
– mooglinux
Aug 12 at 23:13


os.path.isfile





No i didn't how do we execute that?
– astroluv
Aug 12 at 23:17





It looks like that you are trying to create a workflow. If you use more of that kind of rules I would suggest to look in workflow frameworks like snakemake or luigi
– nauer
Aug 13 at 6:45





Isn't this basically a duplicate of your previous question or the one before that? stackoverflow.com/questions/51749269/… it seems like you're still struggling with getting the file processing logic of the same code right. Maybe there's someone local to you who can help? Or if you'd like to move this to a chat room I can help you with your code.
– Iguananaut
Aug 15 at 10:13





Also, when posting Python code please make sure to get the indentation exactly as you have it (you can copy and paste your code directly into the SO editor, select it with the mouse, and press Ctrl-K to indent it all simultaneously)
– Iguananaut
Aug 15 at 10:16




2 Answers
2



Assuming you have the following imports,


import os
import sys
from pathlib import Path



Get the source directory, and resolve the desired path,


src_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
my_dir = src_dir + '\..\repos\notebooks\data\'

path = Path.resolve(Path(src_dir + 'riverbank.jpg'))



To check if a file exists use path.exists(), or something more thorough,


path.exists()


if os.path.isfile(path) and os.access(path, os.R_OK):
print "File exists and is readable"
else:
print "File is missing or unreadable"



Then, you can iterate through a list of files, update a flag and break out if a file cannot be read as follows,


my_files = [ 'riverbank.jpg', ... ]

success = True

for name in my_files:
path = Path.resolve(Path(my_dir + name))
if not (os.path.isfile(path) and os.access(path, os.R_OK)):
success = False
print(path)
break

if success:
print("Files exist and are readable")
else:
print("A file is missing or unreadable")



You could extend this by printing out this file name or by continuing through the file list and providing the user with a list of missing/inaccessible files.



Well, thanks for the help everyone but i managed to solve this problem on my own by using an another approach. So i thought it's better to share how i solved it.


for filename in os.listdir("/home/Desktop/2d_spectra"):
if filename.endswith("_e2ds_A.fits"):
e2ds_hdu = fits.open(filename,ignore_missing_end=True)
e2ds_header = e2ds_hdu[0].header
blaze_file = e2ds_header['HIERARCH ESO DRS BLAZE FILE']
date = e2ds_header['DATE-OBS']
date2 = date = date[0:19]
blaze_file1 = glob(blaze_file)
bis_file = glob('HARPS.' + date2 + '*_bis_G2_A.fits')
ccf_table = glob('HARPS.' + date2 + '*_ccf_G2_A.tbl')

ll = np.size([blaze_file1]) + np.size(bis_file) + np.size(ccf_table) # will convert the string to numpy array
if ll == 3:
#did what i wanted to do.

else:
none






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.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard