Skip to content

Instantly share code, notes, and snippets.

@gubatron
Created May 2, 2013 01:03
Show Gist options
  • Save gubatron/5499514 to your computer and use it in GitHub Desktop.
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
#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