Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

create text in python if not exists

#It will creates Text file in path directory using name of file.txt
import os

path = 'some/path/to/file.txt'
if not os.path.exists(path):
    with open(path, 'w') as f:
    	f.write('Hello, world')
#==== OR ===
mode = 'a' if os.path.exists(path) else 'w'
with open(path, mode) as f:
    f.write('Hello, world!
')
Comment

create text in python if not exists

#Updated dec 2020
#It will creates Text file in path directory using name of file.txt
import os

path = 'some/path/to/file.txt'
if not os.path.exists(path):
    with open(path, 'w') as f:
    	f.write('Hello, world')
#==== OR ===
mode = 'a' if os.path.exists(path) else 'w'
with open(path, mode) as f:
    f.write('Hello, world!
')
Comment

PREVIOUS NEXT
Code Example
Python :: godot code for movement for topdown game 
Python :: python time execution 
Python :: pyspark create empty dataframe 
Python :: define a column as index pandas 
Python :: python get minute from datetime 
Python :: convert dictionary keys to int python 
Python :: python except show error 
Python :: python turtle sierpinski triangle 
Python :: remove base from terminal anaconda 
Python :: import all images from folder python 
Python :: how to install panda3D 
Python :: python pandas trim values in dataframe 
Python :: count words python 
Python :: django filter not null 
Python :: remove unicode from string python 
Python :: sparksession pyspark 
Python :: python for looop array value and index 
Python :: maximizar ventana tkinter python 
Python :: python image read 
Python :: number of times a value occurs in dataframne 
Python :: get rid of axes numbers matplotlib 
Python :: Could not locate a bind configured on mapper mapped class class-tablename, SQL expression or this Session. 
Python :: identity matrix in python 
Python :: how to take user input in a list in python 
Python :: open a filename starting with in python 
Python :: pyttsx3 speech to mp3 
Python :: How to set "Unnamed: 0" column as the index in a DataFrame 
Python :: python gt index in for cycle 
Python :: django override help text 
Python :: folium python map in full screen 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =