Created
May 2, 2013 01:03
-
-
Save gubatron/5499514 to your computer and use it in GitHub Desktop.
Remove trailing slashes on a list or several variables using list comprehension. #python #listComprehension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#let's say you receive several variables and you want none of them to have trailing slashes | |
backup_name='mysql/' | |
backup_source_path='/var/lib/mysql/' | |
backup_destination_path='/mnt/nas/mysql_backups/' | |
#you can put them in a tuple and use list comprehension to operate on all of them | |
#and reassign them | |
(backup_name, backup_source_path, backup_destination_path) = [ x.rstrip('/') for x in (backup_name, backup_source_path, backup_destination_path)] | |
#if we print the values now, no trailing slash on any of them | |
>>> backup_name, backup_source_path, backup_destination_path | |
('mysql', '/var/lib/mysql', '/mnt/nas/mysql_backups') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment