39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
import os
|
|
|
|
def get_file_with_most_lines(directory):
|
|
max_lines = 0
|
|
target_file = ""
|
|
|
|
for filename in os.listdir(directory):
|
|
filepath = os.path.join(directory, filename)
|
|
if os.path.isfile(filepath):
|
|
with open(filepath, 'r') as f:
|
|
line_count = sum(1 for _ in f)
|
|
if line_count > max_lines:
|
|
max_lines = line_count
|
|
target_file = filename
|
|
|
|
return target_file
|
|
|
|
def copy_file(src_directory, dest_directory, filename):
|
|
src_filepath = os.path.join(src_directory, filename)
|
|
dest_filepath = os.path.join(dest_directory, 'data.txt')
|
|
|
|
os.makedirs(dest_directory, exist_ok=True)
|
|
|
|
with open(src_filepath, 'r') as src_file:
|
|
with open(dest_filepath, 'w') as dest_file:
|
|
dest_file.write(src_file.read())
|
|
|
|
def main():
|
|
src_directory = '/var/data'
|
|
dest_directory = '/var/result'
|
|
|
|
target_file = get_file_with_most_lines(src_directory)
|
|
if target_file:
|
|
copy_file(src_directory, dest_directory, target_file)
|
|
print(f"File {target_file} copied to {dest_directory}/data.txt")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|