Why is my condition in python not being met
<p>I have a list of filenames sorted by creation date. These files contain a datetime in the filename for their creation date time. I am attempting to create a sub list for all files after a certain time. </p>

<p>Full list of files -</p>

<pre><code>Allfilenames = ['CCN-200 data 130321055347.csv',
'CCN-200 data 130321060000.csv',
'CCN-200 data 130321063235.csv',
'CCN-200 data 130321070000.csv',
'CCN-200 data 130321080000.csv',
'CCN-200 data 130321090000.csv',
'CCN-200 data 130321100000.csv',
'CCN-200 data 130321110000.csv',
'CCN-200 data 130321120000.csv',
'CCN-200 data 130321130000.csv',
'CCN-200 data 130321140000.csv',
'CCN-200 data 130321150000.csv']
</code></pre>

<p><code>positions [19:24]</code> give the time in format hhmmss. I am using</p>

<pre><code>filenames = [s for s in Allfilenames if os.path.basename(s)[19:24] &gt;= TOffRound]

TOffRound = "080000"
</code></pre>

<p>The result should be a list of all filenames created  on or after or 08:00:00, however the resulting list is missing the "080000" file.</p>

<pre><code>filenames = ['CCN-200 data 130321090000.csv',
'CCN-200 data 130321100000.csv',
'CCN-200 data 130321110000.csv',
'CCN-200 data 130321120000.csv',
'CCN-200 data 130321130000.csv',
'CCN-200 data 130321140000.csv',
'CCN-200 data 130321150000.csv']
</code></pre>

<p>Why is the conditional not returning true on the = part of the condition and returning 'CCN-200 data 130321080000.csv' in my list? Please note I have only shown the basename here for clarity. </p>

<p>The problem with the code given, as suggested by others, is that you are missing the last digit. In terms of slicing a list, the "stop" number given after the : is not considered.</p>

<pre><code>(eg):
&gt;&gt; a = "hello world"
&gt;&gt; print a[0:4]
hell
&gt;&gt; print a[0:5]
hello
</code></pre>

<p>So, change this line in your code and you are good to go:</p>

<pre><code>filenames = [s for s in Allfilenames if os.path.basename(s)[19:25] &gt;= TOffRound]
</code></pre>

<p>However, what you are doing does not scale at all. This is not easier to maintain nor work with any file that is a even a slightly different. The code can be transformed like this:</p>

<pre><code>def filter_files(file_list, TOffRound):
    text_length = len(TOffRound)
    return [file_name for file_name in file_list if file_name[-text_length:] &gt;= TOffRound]
</code></pre>

<p>This will work, irrespective of the size of the file name.</p>

<p>I would also suggest you to get the list of files based on their modification time, that can be taken using <code>os.stat</code> or <code>os.path.getmtime</code>, and act accordingly, rather than using the file name. File name is a string and even though it can support you with older or newer files, it is generally, not a good idea to use that way. You are converting a time stamp to string for the file name. Then this string is converted back to time stamp and convert in the normal case. Instead, if you go for file modification time, you can stay only with the date and time formats rather than the conversions that need be done. This has few advantages:</p>

<ul>
<li>File name or any explicit parameter can change over time but you need not change the logic again and again</li>
<li>File based time stamps do exist for these kind of purposes. So they do provide more control. For instance, If you wish to select files of a certain range, created or modified only at a specific time period? Easy to do with file time stamps.</li>
<li>This splits the time logic from the file names and thus you can name them more meaningfully regarding their purposes thereby simplifying the maintenance of the code over a period of time.</li>
</ul>

python