Rewrite in class, cleaner
This commit is contained in:
23
file_type.py
Normal file
23
file_type.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# file_type.py
|
||||
# Identify type of file: file, directory, link, broken link
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
class FileType():
|
||||
|
||||
@classmethod
|
||||
def get_file_type(cls, filename):
|
||||
# Return type of file ("file", "dir", "link", "broken-link", "other")
|
||||
p = Path(filename)
|
||||
if not p.exists():
|
||||
if p.is_symlink():
|
||||
return "broken-link"
|
||||
else:
|
||||
raise FileNotFound(errno.ENOENT, os.strerror(errno.ENOENT), filename)
|
||||
if p.is_symlink():
|
||||
return "symlink"
|
||||
if p.is_dir():
|
||||
return "directory"
|
||||
elif p.is_file():
|
||||
return "file"
|
||||
return "other"
|
||||
Reference in New Issue
Block a user