What does the variable os.sep matter?



  • What role does the variable os.sep play in this violin and what does it need? I'd like to hear a detailed answer, thank you in advance!

    import os
    import time
    

    source = '"C:\my documents"'

    target_dir = 'D:\backup\'
    target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'

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

    if os.system(zip_command) == 0:
    print('Резервная копия успешно создана в', target)
    else:
    print('Создание резервной копии не удалось')



  • You don't have to use it. os.sep to shape the file. Better use it. https://docs.python.org/3/library/os.path.html#os.path.join :

    from datetime import datetime as DT
    

    target_dir = 'D:\backup'
    target = os.path.join(target_dir, f"{DT.now():%Y%m%d%H%M%S}.zip")


Log in to reply
 

Suggested Topics

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