xam1816
import configparser
from pathlib import Path, PureWindowsPath
def get_current_file_path(xtv_file):
config = configparser.ConfigParser()
with open(xtv_file, ‘r’, encoding='1251') as file:
config.read_file(file)
current_file_path = config.get('XTV', ‘CurrentFile’, fallback=None)
return current_file_path
def get_filename_from_path(path):
path = Path(PureWindowsPath(path))
return path.name
def main():
current_path = get_current_file_path('XtvStatus.txt')
if current_path:
filename = get_filename_from_path(current_path)
print(filename)
else:
print('путь не найден')
if __name__ == ‘__main__’:
main()
скажите правильное ли решение сохранить
import configparser
from pathlib import Path, PureWindowsPath
def get_current_file_path(xtv_file):
config = configparser.ConfigParser()
with open(xtv_file, ‘r’, encoding='1251') as file:
config.read_file(file)
current_file_path = config.get('XTV', ‘CurrentFile’, fallback=None)
return current_file_path
def get_filename_from_path(path):
path = Path(PureWindowsPath(path))
return path.name
def main():
current_path = get_current_file_path('XtvStatus.txt')
if current_path:
filename = get_filename_from_path(current_path)
print(filename)
else:
with open('file.txt', mode='w') as f:
print('путь не найден', filename=f) if __name__ == ‘__main__’:
main()