How to place the created .txt file in a specific folder. Which is created in the same script. Folder name changes by date.

from netmiko import ConnectHandler with open('devices.txt') as f: devices = f.read().splitlines() for ip in devices: cisco_device = { 'device_type': 'cisco_ios', 'ip': ip, 'username': 'cisco', 'password': 'cisco', 'port': 22, 'secret': 'cisco', 'verbose': True } print('Connecting to' + ip) connection = ConnectHandler(**cisco_device) print('Entering enable mode ...') connection.enable() output = connection.send_command('show run') #print(output) prompt = connection.find_prompt() #print(prompt) hostname = prompt[:-1] #print(hostname) import os import datetime _now = datetime.datetime.now() # _today = str(_now.year) + '-' + str(_now.month) + '-' + str(_now.day) def createFolderForTiff(folder_name, path): _fold_name = folder_names _path = path if not (os.path.exists(_path + '/' + _fold_name)): os.chdir(_path) os.mkdir(_fold_name) # else: # print('Папка с именем' + folder_names + 'уже существует') folder_names = str(_now.year) + '-' + str(_now.month) + '-' + str(_now.day) pathh = 'E:/back/' for name in folder_names: createFolderForTiff(name, pathh) now = datetime.datetime.now() today = str(now.year) + '-' + str(now.month) + '-' + str(now.day) file = today + '-' + hostname + '.txt' with open(file, 'w') as backup: backup.write(output) print('Backup of' + hostname + 'completed successfuly') print('#' * 30) connection.disconnect() 

`

  • What is the problem? How to create a folder? How to connect folder and file name? - Enikeyshchik
  • docs.python.org/3/library/shutil.html#shutil.move - this is if you first created the file, and then you want to move it to the folder you created later. - ⷶ ⷩ ⷮ ⷪ ⷩ

0