0

enter image description hereI'm trying to save a file on DISK E of the Windows server and save it in another folder in the same location using Python. But I'm always getting this error:enter image description here

OSError: [WinError 123] The syntax for the file name, directory name, or volume label is incorrect: 'E:\x07utomatic_backups\Auto_backup_mysql.sql'

My Python code:

import os
import shutil
import uuid

source = 'E:\automatic_backups\Auto_backup_mysql.sql' 
files = os.listdir(source)

newLOcal = 'E:\automatic_backups\backup'

for filename in os.listdir(source):
    
    if filename == "Auto_backup_mysql.sql":
        uuid_code = uuid.uuid4().hex
        backupName = uuid_code + '_' + filename        
        src = os.path.join(source,filename)         
        dst = os.path.join(newLOcal, backupName)
        shutil.copy(src, dst)`

I would like to save a file from automatic_backups folder for backup.

Alekis
  • 1
  • 1

2 Answers2

2

Try to use raw strings when dealing with path names as strings in windows, so instead of

source = 'E:\automatic_backups\Auto_backup_mysql.sql'

Use

source = r'E:\automatic_backups\Auto_backup_mysql.sql'
newLOcal = r'E:\automatic_backups\backup'

That is because \ followed by a character is treated as a special character. For example, \n is a new line. Adding the r before the string overrides this behavior.

  • Please refrain from answering common duplicates. Once you earn enough reputation, you will be able to vote to close as duplicate; until then, you can leave a comment pointing to the proper canonical. https://sopython.com/canon/ has a list of common canonicals. – tripleee Mar 16 '23 at 15:55
-2

You're passing a file path instead of a directory path to os.listdir.

Change source to be = 'E:\automatic_backups

source = 'E:\automatic_backups
  • i am getting the following error. errorNotADirectoryError: [WinError 267] The directory name is invalid: 'E:\\automatic_backups\\Auto_backup_mysql.sql' Is it because of windows server? – Alekis Mar 16 '23 at 17:27
  • I'm using: source = r'E:\automatic_backups\Auto_backup_mysql.sql' files = os.listdir(source) newLocal = r'E:\automatic_backups\backup' – Alekis Mar 16 '23 at 17:28