Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cannot get read of a "new line"...

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
0
0
US
Hi,
I hope everyone is well and healthy and staying at home!
And for some reason when I posted my code it removed the "tab" before some lines. Sorry about that!

I tried to use <code> button and the "Superscript" and "Subscript" still the snippet lost it's "tabs'.

Anyway, I have a strange problem, I have a file with the host's names like this:

FJ04TVVP0001NE
FJ04TVVP0002NE
FJ04TVVP0003NE

I need to read each line and add “\c$” to the end of each host to have lines like this:
FJ04TVVP0001NE\c$

For some reason, my code cannot add the “\c$” to the end of the string,
print out looks like this:
FJ04TVVP0001NE
\c$
FJ04TVVP0002NE
\c$
FJ04TVVP0003NE
\c$
The “\c$” always starts from a new line.
Here is the code I wrote:

Code:
st = "\c$"
ds = "\d$"
new_Batch = open ("C:/Scripts/NE_Col/Batch.txt","w")

f_EN_names = " C:/Scripts/NE_Col//NE_Col_names.txt"
with open(f_EN_names) as mfile:
for ln_f in mfile:
ln_f.rstrip()
print (ln_f+st)
new_Batch.write("NET USE \\\\"+ln_f+st+/u:Some Stuff Here”)

mfile.close()
new_Batch.close()
 
Hi

I am afraid you misread the documentation of [tt]str.rstrip()[/tt] :
str.rstrip() said:
[highlight]Return a copy[/highlight] of the string with trailing characters removed.
So only a copy of the original string is altered, which is returned. The original string is kept unaltered :
Python:
>>> str = "foo\n"

>>> str [gray]# original[/gray]
'foo\n'

>>> str.rstrip() [gray]# altered & returned[/gray]
'foo'

>>> str [gray]# still the original[/gray]
'foo\n'

So try something like this :
Code:
st [teal]=[/teal] [i][green]"\c$"[/green][/i]
ds [teal]=[/teal] [i][green]"\d$"[/green][/i]
new_Batch [teal]=[/teal] [COLOR=orange]open[/color] [teal]([/teal][i][green]"C:/Scripts/NE_Col/Batch.txt"[/green][/i][teal],[/teal][i][green]"w"[/green][/i][teal])[/teal]

f_EN_names [teal]=[/teal] [i][green]" C:/Scripts/NE_Col//NE_Col_names.txt"[/green][/i]
with [COLOR=orange]open[/color][teal]([/teal]f_EN_names[teal])[/teal] as mfile[teal]:[/teal]
    [b]for[/b] ln_f [b]in[/b] mfile[teal]:[/teal]
        [highlight]ln_f [teal]=[/teal] [/highlight]ln_f[teal].[/teal][COLOR=orange]rstrip[/color][teal]()[/teal]
        [b]print[/b] [teal]([/teal]ln_f[teal]+[/teal]st[teal])[/teal]
        new_Batch[teal].[/teal][COLOR=orange]write[/color][teal]([/teal][i][green]"NET USE \\\\"[/green][/i][teal]+[/teal]ln_f[teal]+[/teal]st[teal]+[/teal][i][green]"/u:Some Stuff Here\n"[/green][/i][teal])[/teal]

mfile[teal].[/teal][COLOR=orange]close[/color][teal]()[/teal]
new_Batch[teal].[/teal][COLOR=orange]close[/color][teal]()[/teal]

( BTW, place the [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags around your code. )


Feherke.
feherke.github.io
 
It works great! I just starting coding in Python and missing a few things here and there. Thank you for your help dude! [bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top