Matteo Secco Matteo Secco - 1 year ago 53
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")

Answer Source

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")