Search code examples
pythonvariablesusingfile-rename

Rename a file using variables in the program - Python


I want to rename a file called decon.out using two variables in my program. So far I have

gwf = input ("Enter value: ")
myList = os.listdir('.')
    for myFile in myList:
        if re.match("^HHEMQZ", myFile):
            numE = myFile
        elif re.match("^HHNMQZ", myFile):
            numN = myFile
        else: 
            den = myFile
os.rename('decon.out', 'RF'+gwf+''+numE+'')

For example, gwf = 2.5 and numE = HHEMQZ20010101

I would then want decon.out to be renamed as RF2.5HHEMQZ20010101 where RF will always be the same. Currently when I run the script I get an error:

Traceback (most recent call last):
  File "RunDeconv.py", line 77, in <module>
    os.rename('decon.out', 'RF'+gwf+''+numE+'')
TypeError: cannot concatenate 'str' and 'float' objects

Any suggestions?


Solution

  • Use raw_input() instead, input() interprets the input values as Python code turning your 2.5 input into a float number.