I'm a bit new to Python and sort of learning on my own. I wrote a small function to help me find the latest file in a directory. Taking a step back, it reads a bit janky and I was curious what steps or what resources I could look into to help me make this more friendly. Should I be returning False
? Or 0
?
Inside my example/files directory are 3 files which were, for this example, created on the dates specified in the file name:
example/files/randomtext011.201602012.txt
example/files/randomtext011.201602011.txt
example/files/randomtext011.201602013.txt
import os.path
import glob
import datetime
dir = 'example/files'
file_pattern = 'randomtext011.*.txt'
def get_latest_file(file_pattern,path=None):
if path is None:
list_of_files = glob.glob('{0}'.format(file_pattern))
if len(list_of_files)> 0:
return os.path.split(max(list_of_files, key = os.path.getctime))[1]
else:
list_of_files = glob.glob('{0}/{1}'.format(path, file_pattern))
if len(list_of_files) > 0:
return os.path.split(max(list_of_files,key=os.path.getctime))[1]
return False
I'm a bit new to Python and sort of learning on my own. I wrote a small function to help me find the latest file in a directory. Taking a step back, it reads a bit janky and I was curious what steps or what resources I could look into to help me make this more friendly. Should I be returning False
? Or 0
?
Inside my example/files directory are 3 files which were, for this example, created on the dates specified in the file name:
example/files/randomtext011.201602012.txt
example/files/randomtext011.201602011.txt
example/files/randomtext011.201602013.txt
import os.path
import glob
import datetime
dir = 'example/files'
file_pattern = 'randomtext011.*.txt'
def get_latest_file(file_pattern,path=None):
if path is None:
list_of_files = glob.glob('{0}'.format(file_pattern))
if len(list_of_files)> 0:
return os.path.split(max(list_of_files, key = os.path.getctime))[1]
else:
list_of_files = glob.glob('{0}/{1}'.format(path, file_pattern))
if len(list_of_files) > 0:
return os.path.split(max(list_of_files,key=os.path.getctime))[1]
return False
I'm a bit new to Python and sort of learning on my own. I wrote a small function to help me find the latest file in a directory. Taking a step back, it reads a bit janky and I was curious what steps or what resources I could look into to help me make this more friendly. Should I be returning False
? Or 0
?
Inside my example/files directory are 3 files which were, for this example, created on the dates specified in the file name:
example/files/randomtext011.201602012.txt
example/files/randomtext011.201602011.txt
example/files/randomtext011.201602013.txt
import os.path
import glob
import datetime
dir = 'example/files'
file_pattern = 'randomtext011.*.txt'
def get_latest_file(file_pattern,path=None):
if path is None:
list_of_files = glob.glob('{0}'.format(file_pattern))
if len(list_of_files)> 0:
return os.path.split(max(list_of_files, key = os.path.getctime))[1]
else:
list_of_files = glob.glob('{0}/{1}'.format(path, file_pattern))
if len(list_of_files) > 0:
return os.path.split(max(list_of_files,key=os.path.getctime))[1]
return False
I'm a bit new to Python and sort of learning on my own. I wrote a small function to help me find the latest file in a directory. Taking a step back, it reads a bit janky and I was curious what steps or what resources I could look into to help me make this more friendly. Should I be returning False
? Or 0
?
Inside my example/files directory are 3 files which were, for this example, created on the dates specified in the file name - I'm using the date in the file name in my logic:
example/files/randomtext011.201602012.txt
example/files/randomtext011.201602011.txt
example/files/randomtext011.201602013.txt
import os.path
import glob
import datetime
dir = 'example/files'
file_pattern = 'randomtext011.*.txt'
def get_latest_file(file_pattern,path=None):
if path is None:
list_of_files = glob.glob('{0}'.format(file_pattern))
if len(list_of_files)> 0:
return os.path.split(max(list_of_files, key = os.path.getctime))[1]
else:
list_of_files = glob.glob('{0}/{1}'.format(path, file_pattern))
if len(list_of_files) > 0:
return os.path.split(max(list_of_files,key=os.path.getctime))[1]
return False
I'm a bit new to Python and sort of learning on my own. I wrote a small function to help me find the latest file in a directory. Taking a step back, it reads a bit janky and I was curious what steps or what resources I could look into to help me make this more friendly. Should I be returning False
? Or 0
?
Inside my example/files directory are 3 files which were, for this example, created on the dates specified in the file name - I'm using the date in the file name in my logic:
example/files/randomtext011.201602012.txt
example/files/randomtext011.201602011.txt
example/files/randomtext011.201602013.txt
import os.path
import glob
import datetime
dir = 'example/files'
file_pattern = 'randomtext011.*.txt'
def get_latest_file(file_pattern,path=None):
if path is None:
list_of_files = glob.glob('{0}'.format(file_pattern))
if len(list_of_files)> 0:
return os.path.split(max(list_of_files, key = os.path.getctime))[1]
else:
list_of_files = glob.glob('{0}/{1}'.format(path, file_pattern))
if len(list_of_files) > 0:
return os.path.split(max(list_of_files,key=os.path.getctime))[1]
return False
I'm a bit new to Python and sort of learning on my own. I wrote a small function to help me find the latest file in a directory. Taking a step back, it reads a bit janky and I was curious what steps or what resources I could look into to help me make this more friendly. Should I be returning False
? Or 0
?
Inside my example/files directory are 3 files which were, for this example, created on the dates specified in the file name:
example/files/randomtext011.201602012.txt
example/files/randomtext011.201602011.txt
example/files/randomtext011.201602013.txt
import os.path
import glob
import datetime
dir = 'example/files'
file_pattern = 'randomtext011.*.txt'
def get_latest_file(file_pattern,path=None):
if path is None:
list_of_files = glob.glob('{0}'.format(file_pattern))
if len(list_of_files)> 0:
return os.path.split(max(list_of_files, key = os.path.getctime))[1]
else:
list_of_files = glob.glob('{0}/{1}'.format(path, file_pattern))
if len(list_of_files) > 0:
return os.path.split(max(list_of_files,key=os.path.getctime))[1]
return False