Is there a way to append to a file and read it with the same "open" file command in Python3.7? I mean I do not want to have two open statements, one for open("//", "a") and one for open("//", "r"). What I am trying to achieve is run a script which appends the output to the file, and then read the last line of the file. "a+" does not help; it gives a index of out range for readlines()[-1].
Thanks in advance.
Opening the file in a+ makes the file pointer point to the end of the file, which makes it hard to read the last line. You can instead open the file in r+ mode, iterate over the file object until you obtain the last line, and then append the additional output to the file:
with open('file', 'r+') as file:
for line in file:
pass
file.write(output)
# variable line now holds the last line
# and the file now has the content of output at the end
Related
I would like to seek for your help on how to write a simple bash script that reads an input file (e.g. txt file) that has a list into it (e.g. numbers/letters), process it and use that list to delete single/multiple lines on a different txt file. I tried using sed with no luck.
Hope someone can help me...
Thanks in advance and more power!
To read a file, you can use the following code:
#!/bin/bash
file="$1"
while read line
do
#process each line
done < file
I am using Shell scripting. I tried the below option.
I want to append the part of a file to another part of a file.
I tried this command to append the content of a file.
Command:
/bin/cat ../../../test_op.txt/sql/part_code.txt >> ../../../PartitioningUtility/log/test_op.txt
To append the text of part_code.txt to test_op.txt.
No errors but the text is not appending.
Any Solution to this problem.
Any other option to append the data of a one file to another
If you want to append all content from '/bin/cat ../../../test_op.txt/sql/part_code.txt' to '../../../PartitioningUtility/log/test_op.txt' you could add 'cat' in the beginning:
cat ../../../test_op.txt/sql/part_code.txt >> ../../../PartitioningUtility/log/test_op.txt
That is assuming that file paths are correct.
I have a text file, each line is one or more file paths separated with space, all the file has suffix dl, e.g.
/some/path/file.dl
/some/other/path/file2.dl /some/other/path2/file3.dl
/some/other/path3/file4.dl /some/other/path4/file5.dl ...
...
Now I need to transform the above file to another text file. Only the first file of every line should be changed to /out/P{fileName}.h:, {fileName} is the original file name without directory and suffix. e.g.
/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path2/file5.dl ...
...
So how can I write the linux shell script?
Try this command:
$ sed -r 's#^\S*/(\S*)\.dl#/out/P\1.h:#' input
/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path4/file5.dl
I copy pasted some enum values from my IntelliJ IDE in windows to notepad, saved the file in a shared drive, then opened it up in a linux box. When I did cat -A on the file it showed something like:
A,B,C,^M$
D,E,F,^M$
G,H,I,^M$
After searching around I figured that ^M is the carriage return and $ means the last line of the file. I'm just puzzled at how this file is able to have multiple $'s.
From man cat on my GNU box:
-A, --show-all
equivalent to -vET
(snip)
-E, --show-ends
display $ at end of each line
Thus, there are multiple $s because there are multiple lines, each with an end.
$ is the end of line marker with cat -A, not end of file.
This is indicating the file has Windows-style line endings (carriage return followed by line feed) and not Unix-style (only line feed).
(You can convert text files from one format to the other using the programs dos2unix or unix2dos.)
I am new to Linux and have a challenging task.
I have 3 data files, and need to do the following:
Go to line 31 of file 1, delete it
Read 1 line from file 2 and add in place of deleted line
Go to line 97 of file 1 delete it and then read the line 1 from file 2 and add in place of that deleted line in file 1.
The thing is also important to keep the same file i.e file , it is not to be changed.
I tried different versions of sed and perl, with buffer copying tricks but was not successful.
I am open for all suggestions and request the experts to give me suggestions.
I cannot find a reference to the 3rd file in your question, but if you mean replace line number 31 of file 1 with the 1st line of file 2, and replace line number 97 of file 1 with the 2nd line of file 2:
sed -i -e '30R f2
31d;96R f2
97d' f1
The new lines are important after f2 so sed knows that it is the end of the file name.
Note that the R command is a GNU extension, it is not standard.