Write a function concatenate_files(filename1, filename2, new_filename) that concatenates the text from two source files such that the text from the file named by argument filename2 follows the text from filename1. The concatenated text should be written to a new file with the name given by new_filename. Your function must not return anything.We have provided sample input files named part1.txt and part2.txt containing a portion of the text from the novel Alice in Wonderland to test your function.
Question
Write a function concatenate_files(filename1, filename2, new_filename) that concatenates the text from two source files such that the text from the file named by argument filename2 follows the text from filename1. The concatenated text should be written to a new file with the name given by new_filename. Your function must not return anything.We have provided sample input files named part1.txt and part2.txt containing a portion of the text from the novel Alice in Wonderland to test your function.
Solution
Here is a Python function that does what you asked for:
def concatenate_files(filename1, filename2, new_filename):
with open(filename1, 'r') as file1, open(filename2, 'r') as file2:
data1 = file1.read()
data2 = file2.read()
with open(new_filename, 'w') as new_file:
new_file.write(data1 + "\n" + data2)
This function works as follows:
- It opens
filename1andfilename2in read mode and reads their contents intodata1anddata2respectively. - It then opens
new_filenamein write mode and writes the contents ofdata1anddata2into it, separated by a newline character.
You can test this function with your provided files like this:
concatenate_files('part1.txt', 'part2.txt', 'combined.txt')
This will create a new file named combined.txt that contains the concatenated text from part1.txt and part2.txt.
Similar Questions
Copy contents of one file to another fileWrite a Python program that copies the content from one file to another file.Note: Take the input file name from the user.Aim:To create a Python program that reads a text file, copies its contents to another file, and prints the contents to the console.Algorithm:Step 1: Accept a filename input from the user and store it in the variable file.Step 2: Open the input file (file) in read mode and the output file ('OutputData3.txt') in write mode using the open() function.Step 3: Iterate through each line in the input file (fin).Write each line to the output file (fout).Step 4: Close both the input and output files using the close() method.Step 5: Open the output file ('OutputData3.txt') in read mode and store it in the variable fin.Step 6: Iterate through each line in the output file (fin).Print each line to the console.Step 7: Close the output file (fin).Step 8: End the program.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtThis·is·this.That·is·this.·That·is·that.·This·is·That.Now·tell·me·what·is·that?And·what·is·this?Confused?Haha!!Test Case 2:Expected Output:Enter·file·name:·InputData2.txtIn·a·coastal·town,·a·lighthouse·stood·tall,holding·a·magical·secret.·The·lighthousewhispered·stories·to·the·sea,·guidinglost·ships·and·providing·comfort·toweary·sailors.
Copy a file named data1.txt from /input_data to /process_data, move a file named data2.txt from /input_data to /archive_data, and then merge all files in /process_data into a single local file named merged_data.txt.
This task involves creating a lexicon that can store your collection of Word objects, and populating it with words loaded from two text files (in1.txt and in2.txt).More details on each of these steps is provided below, however in short, for this task you should:Create and initialize a variable that represents your lexicon.Define a function read_data() that can read words from a text file into your lexicon (Keeping in mind the constraints from the assignment brief).Call the read_data() function to load all words from the text files in1.txt and in2.txt into your lexicon.Step 1:It is up to you to decide on how you want to represent your lexicon. It may be useful to refer back to the labs to see how we represented collections of Person objects for the sorting algorithms that you implemented.Step 2:In this step you should define a function read_data() that reads words from a text file into your lexicon.This function should take two arguments, the lexicon that words should be loaded into, and the name of the file to load.From the assignment brief, the criteria for a word is:A word is obtained as a sequence of characters separated by whitespaceWords should be stored in lowercaseAny numbers or punctuation characters should be removedUnique words should only be stored once in the lexicon (You can use the frequency of the word to show that a word appears twice in the files)Hint:Below is an example of how you can remove all punctiation and numeric characters from a string:my_str = 'H1-ell.0o'# Remove all punctuation and numeric charactersmy_str = ''.join([ch for ch in my_str if ch.isalpha()])print(my_str)>>> HelloDo not be concerned with initializing the neighbours per-word. This will be done in Task 4.Step 3.In this step, you should make two calls to your read_data() function to load both in1.txt and in2.txt into your lexicon. When calling these functions, you can hardcode the names of these files.
Write a program that copies the content of a file to another file.Usage: cp file_from file_toif the number of argument is not the correct one, exit with code 97 and print Usage: cp file_from file_to, followed by a new line, on the POSIX standard errorif file_to already exists, truncate itif file_from does not exist, or if you can not read it, exit with code 98 and print Error: Can't read from file NAME_OF_THE_FILE, followed by a new line, on the POSIX standard errorwhere NAME_OF_THE_FILE is the first argument passed to your programif you can not create or if write to file_to fails, exit with code 99 and print Error: Can't write to NAME_OF_THE_FILE, followed by a new line, on the POSIX standard errorwhere NAME_OF_THE_FILE is the second argument passed to your programif you can not close a file descriptor , exit with code 100 and print Error: Can't close fd FD_VALUE, followed by a new line, on the POSIX standard errorwhere FD_VALUE is the value of the file descriptorPermissions of the created file: rw-rw-r--. If the file already exists, do not change the permissionsYou must read 1,024 bytes at a time from the file_from to make less system calls. Use a bufferYou are allowed to use dprintf
file1.txt est créé.file2.txt est créé.Les fichiers 1.txt et 2.txt sont détruits.Rien n'est fait puisque le répertoire est vide et que make dépend de file1.txt et file2.txt.file1.txt et file2.txt sont créés.Rien ne se passe puisque file2.txt existe déjà.Les fichiers 1.txt et 2.txt sont détruits.file1.txt est créé.file1.txt est créé.Rien ne se passe puisque file1.txt et file2.txt existent déjà
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.