Python: too many joins()?

62 views Asked by At

I'm not sure why I'm getting this error, I'm using str.join() and os.path.join() at different points in the script, is that the cause?

Using os.path.join:

from os.path import getsize, dirname, join

class Wav:
    src_path = "No path"
    dest_path = destination
    old_name = "name.wav"
    new_name = ""

    def __init__(self, path):
        self.src_path = path
        self.old_name = os.path.split(path)
        self.new_name = self.old_name
        self.dest_path = join(destination, self.new_name) # error here

Here is my error:

Traceback (most recent call last):  
  File "call.py", line 132, in <module>  
    temp = Wav(temp_path)  
  File "call.py", line 32, in __init__  
    self.dest_path = join(destination, self.new_name)  
  File "/usr/lib/python2.7/posixpath.py", line 75, in join  
    if b.startswith('/'):  
 AttributeError: 'tuple' object has no attribute 'startswith'  

Is this a conflict with str.join() or am I not importing os.path properly?

3

There are 3 answers

2
Kevin On BEST ANSWER
self.dest_path = join(destination, self.new_name) # error here

self.new_name is not a string, it's a tuple, so you can't use it as the second argument to join. Perhaps you meant to join destination with just the last element of self.new_name?

self.dest_path = join(destination, self.new_name[1])
0
Eric Renouf On

self.new_name is a tuple, not a string in your code, and os.path.join takes a list of strings, not a string and a list. You could do it with

self.dest_path = join(destination, *self.new_name)

to expand self.new_name as parameters to os.path.join

0
Faminator On
  1. Check if destination and self.new_name are lists or. tuples

Tuples (they look so:

mytup = ('hey','folk')

) have no attribute startswith. You have to use strings. To check this just print the variables out in your program.

  1. I don't think join replaces each other but you should try it.

    import os.path

Write this under your first import line. Then replace

join(...,...)

with

os.path.join(...,...)

if you want to use the os.path module.