简体   繁体   中英

A byte of python - zip program (unexpected character after line continuation character)

I'm doing the first program of A Byte of Python (python3) for the first zip program on my windows7 using Python 3.6.2 I successfully added zip into my system environment/variables, but found a issue. Here is the code:

# Try to back up something using Python

import os
import time

source = [r'H:\Statistics\Vim']

target_dir = r'H:\Statistics\SQL'

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

if not os.path.exists(target_dir):
    os.mkdir(target_dir)

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

# Run the backup
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command) == 0:
    print('Successful backup to', target)
else:
    print('Backup FAILED')

This is the error:

target = target_dir + os.sep + \\time.strftime('%Y%m%d%H%M%S') + '.zip'
                                                                      ^
SyntaxError: unexpected character after line continuation character

I don't know why. Hope someone can help. Big thanks!

I think I should delete \\ in the line target = target_dir + os.sep + \\time.strftime('%Y%m%d%H%M%S') + '.zip'

I have a misunderstanding of os.sep

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM