How do you add the possibility of transferring other files and catalogues directly to the command line?



  • I'm studying python on Byte of Python's book and I don't understand how to accomplish the following:

    Another possible improvement would be the possibility of transferring other files and catalogues directly to the command line. These names can be listed. sys.argv and add the source to our list by means of a method to extend the class list.

    And here's my code:

    import os, time as t
    

    Файлы и каталоги, которые необходимо скопировать, собираются в список.

    source = ['"C:\my documents"']

    Резервные копии должны храниться в основном каталоге резерва.

    target_dir = 'D:\backup'

    today = os.path.join(target_dir, t.strftime('%Y%m%d'))

    if not os.path.exists(today):
    os.mkdir(today) # создание каталога, если он отсутствует
    print('Каталог успешно создан', today)

    now = t.strftime('%H%M%S.zip')

    comment = input('Введите имя zip архива --> ')

    if len(comment) == 0: # Если пользователь ничего не ввел, то имя zip архива - now
    target = os.path.join(today, now)
    else:
    target = os.path.join(today, comment.replace(' ', '_') + '.zip')

    Используем команду "zip" для помещения файлов в zip-архив:

    zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))

    Запускаем создание резервной копии:

    if os.system(zip_command) == 0:
    print('Резервная копия успешно создана в', target)
    else:
    print('Создание резервной копии НЕ УДАЛОСЬ')

    The detailed answers are appreciated. Thank you so much for all the answers!



  • import argparse
    def createParser ():   # парсер аргументов коммандной строки
       parser = argparse.ArgumentParser()
       parser.add_argument ('-arg', '--argument')
       return parser
            
    parser = createParser() 
    namespace = parser.parse_args(sys.argv[1:])
        
    print(namespace.argument) # отобразит значение аргумента, в данном случае file.txt
    

    The violin shall be called:

    python script.py -arg file.txt
    


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2