Search code examples
pythonfilepathnew-operatorsys

Creating files in specific path with python


is it possible to create a file with a specific extension, name and python with the sys module or any other module?

Thanks!


Solution

  • This should work with the path and the file name:

    import os
    
    PATH = '/home/user/somepath'
    FILE = 'somefile.someext'
    
    os.makedirs( PATH, exist_ok=True)   # create the PATH if not exists
    
    full_name = os.path.join( PATH, FILE )
    with open( full_name ) as fout :
        print >>fout, 'some message'