Matteo Secco Matteo Secco - 1 month ago 5x
Python Question

How to create a file in a folder

How can i create a

file in a folder in Python?
The name of the folder is example
I tried:

file = open("\example\file.pck","wb")

But it says:

OSError: [Errno 22] Invalid argument: '\example\x0cile.pck'

Solved! The right command is:

file = open("example/file.pck", "wb")


You need to use forward slashes and also it seems that the example folder you trying to access doesn't exist. That's probably because you wanted to enter relative address but you entered an absolute one. So it should be:

open("example/file.pck", "wb")