0

I am writting a script to backup a list of paths, inspired by https://github.com/Johanndutoit/Zip-to-FTP-Backup/blob/master/backup_to_ftp.py

So I have an ini file

[folders]
/home/david/docs
/home/david/images
/home/david/videos

[ftp]
username=etc
password=pwd

The code to read it is:

config = configparser.ConfigParser(allow_no_value=True)
config.optionxform = lambda option: option  # preserve case for letters
config.read('backupcfg.ini')

filelistings = [] # All Files that will be added to the Archive
# Add Files From Locations
for folder in config.options("folders"):
    filelistings.append(str(folder.strip("'")))

The problem is I can't find a way to read it as raw when I'm running it on windows, with folders like

[folders]
Z:\Desktop\winpython

I can't scape the backslash. I've ended up including it as:

filelistings = [r'Z:\docs\winpython', r'Z:\images\family']

Is there any way to write the paths in the ini? Can't find a way to read config.options("folders") to return raw.

Thank you for your help!

I have tried to add the path straight to the tar:

config = configparser.ConfigParser(allow_no_value=True)
    config.optionxform = lambda option: option  # preserve case for letters
    config.read('backupcfg.ini')
    now = datetime.datetime.now()
    zipname = 'backup.' + now.strftime("%Y.%m.%d") + '.tgz'
    
    with tarfile.open(zipname, "w:gz") as tar:
        for name in config.options("folders"):
            print ("Adding "+name)
            tar.add(str(name))

Which reports can't find filename: 'Z' Is there any way to access that information as it is, with ?

  • Why do you say Perl and tag Perl when this is Python? – ikegami Jun 14 '22 at 01:20
  • 1. You could always use `/`. DOS and Windows have always accepted `/` as a dir sep. But, 2. You don't want to escape the `\ `. `\ ` is escaped in string literals to create a string without the escape. ConfigParser surely produces the string as-is. – ikegami Jun 14 '22 at 01:21
  • Ugh, you are right, it's Python - my mistake. My idea is to let the user simply copy and paste paths in the text file as they come out of the file browser. – David Lloret Jun 14 '22 at 21:23

1 Answers1

0

I got the idea for this answer from this answer. The problem isn't with the slashes. It's with the colon (:). By default, Configparser uses = and : as delimiters. But you can specify other delimiters to use instead.

backupcfg.ini

[folders]
/home/david/docs
Z:\Desktop\winpython

python code

import configparser

config = configparser.ConfigParser(allow_no_value=True, delimiters=(','))
config.optionxform = lambda option: option  # preserve case for letters
config.read('backupcfg.ini')

for folder in config.options("folders"):
    print(folder)

output:

/home/david/docs
Z:\Desktop\winpython
bfris
  • 5,272
  • 1
  • 20
  • 37