def c_bat(): global pth #b="path of the directory" pth=os.path.join(b,"Test.txt") z=["asbdsbdhbdh\n","ajdajkdjasjkd\n","adjnajdnakjkjd\n","required_file"] file1=open(pth,"w") file1.writelines(z) file1.close()