1

I have 2 input files that I want to modify and create new files if log file doesnt exist.

All_files_with_path = ['/path/to/some/file/Test/Directory1/Name_xxx.yyy.ddd_Foo.input1', '/path/to/some/file/Test/Directory1/Randomletters.Name_xxx.yyy.ddd_Foo.input2.ext', '/path/to/some/file/Test/Directory1/Name.zzz.aaa.ggg_Foo.input1', '/path/to/some/file/Test/Directory1/Name.zzz.aaa.ggg_Foo.log', '/path/to/some/file/Test/Directory1/Randomletters.zzz.aaa.ggg_Foo.input2.ext', '/path/to/some/file/Test/Directory2/Name2_xxx.yyy.ddd_Foo.input1', 'path/to/some/file/Test/Directory2/Randomletters.Name2_xxx.yyy.ddd_Foo.input2.ext']

[All_file_with_path] contains all the files in a root here: /path/to/some/file/Test/. If the log file exists for some but not all the input1 files, then I want to execute the creation of new files only for those that dont have corresponding logfile.

Format of File 1: Name_xxx.yyy.ddd_Foo.input1 Format of File 2: Randomletters.Name_xxx.yyy.ddd_Foo.input2.ext

Format of logfile: Name_xxx.yyy.ddd_Foo.log

Error: I dont get any compilation errors but my commands[] list is empty. After some digging around, I found it is not generating the new input2.ext files... I know my "exists" condition check is wrong but don't know where or how to change it.

My second half is pretty much this logic : Safely create a file if and only if it does not exist with python

def modify_files(All_files_with_path):
commands = []
logfile = ''
#print(All_files_with_path)
for file in All_files_with_path:
   commandsfilepath = file.rsplit('/Test',1)[0] 
   if not 'NEW' in file:        #I am checking for this because I dont want to create _NEW_NEW and so on
        #print(file)

        if file.endswith('.input1'):
            prefix = file.rsplit('.', 1)[0]
            suffix = file.rsplit('.', 1)[1]
            newfilewithpath = prefix + '_NEW.' + suffix
            logfile = prefix+ '.log'
            exists = os.path.isfile(logfile)

        elif file.endswith('.input2.ext'):
            print(file)
            prefix = file.rsplit('.input2', 1)[0]
            newfilewithpath = prefix + '_NEW.' + 'input2.ext'
        #print(newfilewithpath)

        if exists: 
                print('log file exists. exiting the loop...... ')

                continue

        else:
            print('log file did not exist in the current directory. create NEW files and execute the rest')  
            flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY

            try:
                print('trying to create modifed input1 and input2 files')
                file_handle = os.open(newfilewithpath, flags)
                print('done ')
            except OSError as e:
                if e.errno == errno.EEXIST:  # Failed as the file already exists.
                    pass
                else:  # Something unexpected went wrong so reraise the exception.
                    raise
            else: # No exception, so the file must have been created successfully.
                if file.endswith(".input1"):
                   """ do something"""" 

                elif file.endswith(".input2.txt"):
                    commands.append(newfilewithpath)
                    """do something with input2.txt"""

    return commands

output I want:

commands = ['/path/to/some/file/Test/Directory1/Randomletters.Name_xxx.yyy.ddd_Foo_NEW.input2.ext','path/to/some/file/Test/Directory2/Randomletters.Name_xxx.yyy.ddd_Foo_NEW.input2.ext']
Ram
  • 547
  • 2
  • 4
  • 13
  • I did follow this: https://stackoverflow.com/questions/61497237/python-list-manipulation-to-compare-part-of-the-element-and-not-the-complete-ele to break the input list to contain _Foo and not _Foo names. Then processed it so did not have to check for log file. Instead pass only the list with the difference – Ram Apr 29 '20 at 20:16

0 Answers0