23

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
  • 44,105
  • 12
  • 114
  • 143
Sihan Zheng
  • 465
  • 4
  • 17

3 Answers3

23

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
  • 328,213
  • 58
  • 503
  • 561
5

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(".")

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).

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
ffi
  • 353
  • 4
  • 11
0

If you need to retrieve the filename without extension, you have the os.path.splitext function:

>>> import os
>>> name, extension = os.path.splitext("movies.csv")
>>> name
'movies'
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Joël
  • 2,723
  • 18
  • 36