Copying contents of a file into another in File Handling - while-loop

I want to copy the Python Scripts ignoring the comments in a code to another file. I have tried the below code but its copying all the lines of the code including comments. Please let me know how to rectify it. I am a newbie in Python Programming.
with open("essays.txt",'rb') as f1:
with open("myfile.txt",'wb') as f2:
while True:
buf=f1.readline()
if len(buf)!=0:
if buf[0]=='#':
continue
else:
f2.write(buf)
else:
break
print("File Copied")
The above is copying all the contents of file1 into file2 including the comments which is not required in the output.

I have checked your code is working when the first character is #. Now imagine if there are spaces or tabs at the beginning, then your code will not work.
Please try the below code, strip white spaces and tabs from the left of the string and then check for the first character.
with open("createEntitiesFromDB.py",'rb') as f1:
with open("myfile.txt",'wb') as f2:
while True:
buf=f1.readline()
if len(buf)!=0:
buf2 = buf.lstrip()
if len(buf2)!=0:
if buf2[0]=='#':
continue
else:
f2.write(buf)
else:
f2.write(buf)
else:
break
print("File Copied")

Related

Deleting a line from a multiline flxText

How can I delete the first line from a flxText?
I have tried text.textField.replaceText(0, text.textField.getLineLength(0), ""); but it only works once. I have also tried keeping count of the line that's being deleted and it doesn't work either.
edit: I would like to reopen this question to specify that I want to take into account both newlines and word wrap.
Considering the text contains the line breaks why not just split the text on \n, remove the lines you no longer need, and then set the text to the joined lines.
var lines = flText.text.split("\n");
lines.shift(); // removes the first line
flText.text = lines.join("\n");

Streamwriter adding unwanted characters to the beginning of the line

I've written a program to write out an automated, delimited invoice file, and it seems to work properly, the file looks correct in notepad. However, when the file is received on the other end, there are a couple of extra characters at the beginning.
The code is basically
fWriter = My.Computer.FileSystem.OpenTextFileWriter(OutputPath)
LineString = 'insert line here
fWriter.WriteLine(LineString)`
the screenshot from the client has a three odd shaped characters at the beginning. They aren't in the input string, and I'm lead to believe it's because the OpenTextFileWriter isn't writing ascii, but it's a flat text file, or it's supposed to be.
Any help would be appreciated.

VB.NET - Appending text to the last line of text file leaves (sometimes) an empty line

The intention of the software I am coding is to append a string to the last line of a text file. The problem I am experiencing is that depending on the situation, sometimes I get an empty line when I try to append text to the last line of a text file.
My text file is very simple. It contains a few lines of text and each line contains a few words. When I open the text file with Windows' Notepad (without editing the content of the text file) and I move the caret to the end of the entire file, sometimes the caret is positioned at the end of the last line with content (text), and sometimes it is positioned at an empty line that doesn't contain even a white space.
When the caret's ending position corresponds to the last char of a line that contains text, I don't experience any problem appending some text programmatically. The programmatically appended line is at a new line and there isn't any empty line between the old last line and the current last line in the text file.
The problem appears when I can move the caret in the text file to the last possible position and that position corresponds to an empty line. In that case, when I try to append a new line programmatically, I can see an empty line before the just added line.
I have tried different codes and techniques, but it always happens the same to me.
The code I am actually using is:
If stringLastLine.Length = 0 Then
Using sw As StreamWriter = New System.IO.StreamWriter(filePath, True)
sw.Write("Inserted text")
sw.Close()
End Using
ElseIf stringLastLine.Length > 0 Then
Using sw As StreamWriter = New System.IO.StreamWriter(filePath, True)
sw.Write(Environment.NewLine & "Inserted text")
sw.Close()
End Using
End If
Basically, I am trying to detect if the last line length is over zero, so it's a line with text content and I will use "environment.newline + string". If it's a line without content (length = 0) I add directly the string without using "environment.newline". In the case that length = 0 I always get an empty line in between.
Any idea to solve this problem?
How about trimming your text to ensure there's no end of line characters at the end (the whole text)? Right now you might have an issue with multiple blank lines:
text = text.TrimEnd({Convert.ToChar(10), Convert.ToChar(13)})
This handles the EOL characters.

Not completed file reading

I'm trying to print the number of rows in my file training.txt but found out that my code does not read the whole file. Code goes like this:
row = 1
for line in io.lines 'training.txt' do
row = row + 1
end
print(row)
I tried changing the file with test.txt that has the same format with training.txt and file reading worked fine, reads until the end of file. So maybe the problem is with my text file? But how? It has the same format.
Text files are uploaded here for testing.
The file you uploaded training.txt is not the same format as test.txt, it does not have newline characters that indicate the EOL (End of Line). Try opening it in notepad to see the difference.

Replace a 'bel' character in a file in vb.NET

I've come across a character in one of my data feeds, which I have never encountered before
The images above are the data feed in Notepad++ and Notepad view. As you can see it appears as 'BEL' in Notepad++ and a sort of 'bullet point' in Notepad.
How would I go about replacing this character in vb.NET?
I've tried a simple replace in a SSIS Script Task by copying and pasting the character into the replace function, e.g.
text = text.Replace("copy and pasted character", "")
and this gives this error
All help is extremely appreciated,
Thanks
I’ve got no idea what SSIS is but since you wanted to know a solution in VB.NET, the code you’ve tried will work here. That is:
text = text.Replace("copy and pasted character", "")
will work just fine in VB. Alternatively, you can use the following:
text = text.Replace(Chr(7).ToString(), "")
Find out what the Ascii value of the character is and then use the Chr function to eliminate it
i.e.
text = text.Replace(Chr(n), "")
[Bell] is probably character 7