Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

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
edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
deleted 52 characters in body
Source Link
pyNovice89
  • 143
  • 1
  • 1
  • 5

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
edited body; edited title
Source Link
Mathieu Guindon
  • 75.5k
  • 18
  • 194
  • 467
Loading
Source Link
pyNovice89
  • 143
  • 1
  • 1
  • 5
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /