Advertisement
rajeshinternshala

Untitled

Sep 7th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. def split_files(baseFilename):
  2.     # Define the file extensions and corresponding output file names
  3.     extensions = {'.c': 'c_', '.cpp': 'cpp_', '.cs': 'cs_'}
  4.    
  5.     # Create output files for each extension type
  6.     output_files = {}
  7.     for extension, prefix in extensions.items():
  8.         output_filename = f"{prefix}{baseFilename}.txt"
  9.         output_files[extension] = open(output_filename, 'w')
  10.    
  11.     # Read and process the input file
  12.     with open(baseFilename, 'r') as input_file:
  13.         current_extension = None
  14.         for line in input_file:
  15.             line = line.strip()
  16.             if line.endswith(tuple(extensions.keys())):
  17.                 current_extension = line[line.rfind('.'):].lower()
  18.             elif current_extension:
  19.                 output_files[current_extension].write(line + '\n')
  20.    
  21.     # Close all output files
  22.     for file in output_files.values():
  23.         file.close()
  24.  
  25. # Example usage
  26. baseFilename = "file_00.txt"
  27. split_files(baseFilename)
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement