How to read and write multiple files in Python?

Goal:

I want to write a program for this:
In a folder I have =n= number of files;
first read one file and perform some operation then store result in a separate file.
Then read 2nd file, perform operation again and save result in new 2nd file.
Do the same procedure for n number of files.
The program reads all files one by one and
stores results of each file separately.

solution:

#+BEGIN_SRC Python
import sys
#argv is your commandline arguments, argv[0] is your program name, so skip it

for n in sys.argv[1:]:
print(n) #print out the filename we are currently processing
input=open(n,”r”)
output=open(n,+”.out”,”w”)
# do some processing
input.close()
output.close()
#+END_SRC

– syn :: system-specific parameters and functions
– module :: a file contains Python code

why python module?

Python module is used to group related functions, classes, and variables
for better code management and avoiding name clash

https://stackoverflow.com/questions/208120/how-to-read-and-write-multiple-files

Similar Posts: