Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python file handling

with open('filename', 'a') as f: # able to append data to file
	f.write(var1) # Were var1 is some variable you have set previously
	f.write('data') 
	f.close() # You can add this but it is not mandatory 

with open('filename', 'r') as f: # able to read data from file ( also is the default mode when opening a file in python)

with open('filename', 'x') as f: # Creates new file, if it already exists it will cause it to fail

with open('filename', 't') as f: # opens the file in text mode (also is defualt)

with open('filename', 'b') as f: # Use if your file will contain binary data
  
with open('filename', 'w') as f: # Open file with ability to write, will also create the file if it does not exist (if it exists will cause it to fail)
  
with open('filename', '+') as f: # Opens file with reading and writing

# You can combine these as you like with the + for reading and writing
Comment

python file handling

# a file named "geek", will be opened with the reading mode.
file = open('geek.txt', 'r')
# This will print every line one by one in the file
for each in file:
    print (each)
Comment

PREVIOUS NEXT
Code Example
Python :: program arguments python 
Python :: python check if string is int 
Python :: django creating calculated fields in model 
Python :: read file from s3 python 
Python :: root template 
Python :: pandas transpose 
Python :: How to convert simple string in to camel case in python 
Python :: left click pyautogui 
Python :: should i make tkinter in classes ? , Best way to structure a tkinter application? 
Python :: get string until character python 
Python :: how to import a python function from another file 
Python :: python get volume free space 
Python :: python list of colors 
Python :: how do i print a list line by line in python 
Python :: remove 1st column pandas 
Python :: pandas groupby aggregate multiple columns 
Python :: dictionary to a dataframe pandas arrays must all be same length 
Python :: Scaling Operation in SkLearn 
Python :: python: calculate number of days from today date in a data frame 
Python :: load a Dictionary from File in Python Using the Load Function of the pickle Module 
Python :: vault python client 
Python :: python pair two lists into a dictionary 
Python :: pil image resize not working 
Python :: train_size 
Python :: import word_tokenize 
Python :: random python 
Python :: how to download nltk in python 
Python :: tkinter yes no dialogue box 
Python :: get last element of a list python 
Python :: show all urls django extensions 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =