Question

I've got a little problem with creating file in python. I'd like to create it on a desktop and name it. Than my program should create anoter folder in it and take a name from user, but when I do something like this:

def folder(self):
    home = os.sep.join((os.path.expanduser('~'),'Pulpit'))
    if not os.path.exists(home):
        home = os.path.join((os.path.expanduser('~'), 'Desktop'))
    opto = home + '\OptoMaQ'
    self.directory = opto
    nam = str(self.nmget.get())
    mypath = opto + '\%s'  %nam
    if not os.path.exists(opto):
        os.makedirs(opto)
        if len(nam) == 0:
            self.fold.config(text = 'This name is incorrect',background = 'red')
        else:   
            if not os.path.exists(mypath):
                os.makedirs(mypath)
                self.fold.config(text = 'Folder was created',background = 'green')
            else:
                self.fold.config(text = 'This name is taken',background = 'red')
    else:
        if len(nam) == 0:
            self.fold.config(text = 'This name is incorrect',background = 'red')
        else:   
            if not os.path.exists(mypath):
                os.makedirs(mypath)
                self.fold.config(text = 'Folder was created',background = 'green')
            else:
                self.fold.config(text = 'This name is taken',background = 'red')

It's for GUI programing of course. When i run it, there is an error: " opto = home + '\OptoMaQ' TypeError: can only concatenate tuple (not "str") to tuple". The funny thing is that it worked for me a few days ago and now i doesn't work. Anyone can help me?

Was it helpful?

Solution

Use:

if not os.path.exists(home):
    home = os.path.join(os.path.expanduser('~'), 'Desktop')
opto = home + r'\OptoMaQ'

os.path.join does not take tuple as an argument, os.sep.join does.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top