22

I am using Python 3 to process file names, and this is my code:

name = 'movies.csv'
table_name = name.strip(".csv")

The expected value of table_name should be "movies" yet table_name keeps returning "movie".

Why is it doing this?

vaultah
  • 40,483
  • 12
  • 109
  • 137
Sihan Zheng
  • 455
  • 4
  • 17

3 Answers3

22

strip() removes all the leading and trailing characters from the input string that match one of the characters in the parameter string:

>>> "abcdefabcdefabc".strip("cba")
'defabcdef'

You want to use a regex: table_name = re.sub(r"\.csv$", "", name) or os.paths path manipulation functions:

>>> table_name, extension = os.path.splitext("movies.csv")
>>> table_name
'movies'
>>> extension
'.csv'
Tim Pietzcker
  • 313,408
  • 56
  • 485
  • 544
4

Maybe a bit late, but for those reading this in the future, there is a lazy way that appears to work fine too (assuming all the files from which you want to retrieve the names are CSV files):

if name.endswith('.csv'):    
    table_name = name.rstrip("csv").rstrip(".")

As said in other solutions, the strip() method removes all the leading/trailing characters that match those inside the parentheses. Therefore, the idea in this approach is to:

  1. Remove the csv extension - since there is a . we know rstrip() will stop searching there. This will leave us with the movies. string.
  2. Remove the . from the movies. string - the rstrip() will only look for trailing dots.

Why rstrip(): Since we know that the text to be removed is in the end of the string, we can specify rstrip for better control (i.e. to avoid unintentionally removing any eventual leading c, s or v characters)

ffi
  • 145
  • 1
  • 8
0

I don't know what you need is, but if it's retrieving filename without extension, you have the os.path.splitext function:

>>> import os
>>> name, extension = os.path.splitext("movies.csv")
>>> name
'movies'
Joël
  • 2,569
  • 17
  • 34