Python: Trying to check if file exists and if not create new files and final output list

岁酱吖の 提交于 2020-05-16 22:00:10

问题


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']

来源:https://stackoverflow.com/questions/61472020/python-trying-to-check-if-file-exists-and-if-not-create-new-files-and-final-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!