0
votes

I've been trying to write lines to a file based on specific file names from the same directory, a search of the file names in another log file(given as an input), and the modified date of the files. The output is limiting me to under 80 characters per line.


    def getFiles(flag, file):

        if (flag == True):
            file_version = open(file)
            if file_version:
                s = mmap.mmap(file_version.fileno(), 0, access=mmap.ACCESS_READ)
            file_version.close()

        file = open('AllModules.txt', 'wb')
        for i, values in dict.items():
            # search keys in version file
            if (flag == True):
                index = s.find(bytes(i))
                if index > 0:
                    s.seek(index + len(i) + 1)
                    m = s.readline()
                    line_new = '{:>0}  {:>12} {:>12}'.format(i, m, values)
                    file.write(line_new)
                    s.seek(0)
            else:
                file.write(i +'\n')

        file.close()


    if __name__ == '__main__':
        dict = {}
        for file in os.listdir(os.getcwd()):
            if os.path.splitext(file)[1] == '.psw' or os.path.splitext(file)[1] == '.pkw':
                time.ctime(os.path.getmtime(file))
                dict.update({str(os.path.splitext(file)[0]).upper():time.strftime('%d/%m/%y')})
        if (len(sys.argv) > 1) :
            if os.path.exists(sys.argv[1]):
                getFiles(True, sys.argv[1])
        else:
            getFiles(False, None)

The output is always like:


    BW_LIB_INCL         13.1 rev. 259 [20140425 16:28]
         16/05/14

The interpretation of data is correct, then again the formatting is not correct as the time is put on the next line (not on the same). This is happening to all the lines of my new file.

Could someone give me a hint?

1
minor hint: instead of if (flag == True): just use if flag: see Idiomatic Python. You have extra () in each if statement, and you don't have to compare a value to boolean: any "empty" value is falsy, non-"empty" is truthy. - Peter M. - stands for Monica
Thanks, I'm coming in to python from C++ ... that's why I use (). I have to get rid of this habit. - Bogdan
in general remember that in Python identity is not the same as equality of values (in C/C++ it's totally mixed up). So for instance if you're looking for 'falsy' values (0, False, empty string, etc), it's if flag: ..., while if you are certain that flag has to be checked for single object, e.g. None, use flag is None (identity test) not flag == None (== is value test). This matters, bc e.g. valid result for flag might be empty string ('') while invalid result might be None and you need to distinguish between the two and if flag: ... test will not do that. - LetMeSOThat4U

1 Answers

0
votes

m = s.readline() has \n at the end of line. Then you're doing .format(i, m, values) which writes m in the middle of the string.

I leave it as exercise to the reader to find out what's happening when you're writing such line to a file. :-)

(hint: m = s.readline().rstrip('\n'))