Search code examples
pythonintegertypeerrorrequired

Python: Opening a file with defined drive letter - TypeError: an integer is required


Here is my two lines of code:

drive = 'j:'
f = open("%s\sample", "wb", drive)

I'm trying to write a simple program that is writing to a file that is being created by the program, the file is to be created on a defined drive, in this case, just onto "J:", however the following is displayed when running the code:

Traceback (most recent call last):
File "D:\FYP\Program\usb.py", line 2, in <module>
f = open("%s\sample", "wb", drive)
TypeError: an integer is required

Am I missing something simple here?


Solution

  • drive = 'j:'
    f = open("%s\sample" % drive, "wb")
    

    Guess this is what you want.