Frage

I'm trying to use a QProcess to run a mkdir command (I'm using linux). The process would create a folder on the user's Desktop called 'output'. The code looks like this:

def mkOutput():
    # get the user's environmental variables
    env = QtCore.QProcessEnvironment.systemEnvironment()
    proc = QtCore.QProcess()
    proc.setProcessEnvironment(env)

    # find the HOME variable, append it to args
    HOME = env.value('HOME', defaultValue='./')
    args = "/Desktop/output/"

    args = HOME+args

    proc.setStandardOutputFile('out.txt')
    proc.setStandardErrorFile('err.txt')
    proc.start("mkdir", args)
    proc.waitForFinished()

out.txt is empty while err.txt reads:

/usr/bin/mkdir: cannot create directory ‘/’: File exists
/usr/bin/mkdir: cannot create directory ‘o’: File exists
/usr/bin/mkdir: cannot create directory ‘t’: File exists
/usr/bin/mkdir: cannot create directory ‘p’: File exists
/usr/bin/mkdir: cannot create directory ‘u’: File exists
/usr/bin/mkdir: cannot create directory ‘t’: File exists
/usr/bin/mkdir: cannot create directory ‘/’: File exists

For some reason it's trying to run mkdir on each letter in the argument rather than using the whole string itself. I've tried to use str() to on the args variable but nothing seems to work. Every time it just creates multiple folders in the directory I run the program from.

War es hilfreich?

Lösung

In proc.start("mkdir", args), args should be a list or tuple. Because you are passing a string, PyQt is converting it into a list, making Qt receive a list of each of the characters of the string. To fix the problem, change args = "/Desktop/output/" to args = ("/Desktop/output/",)

BTW, Python has an function for mkdir().

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top