×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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()

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

Hi

I am afraid you misread the documentation of str.rstrip() :

Quote (str.rstrip())

Return a copy 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 :

CODE --> Python

>>> str = "foo\n"

>>> str # original
'foo\n'

>>> str.rstrip() # altered & returned
'foo'

>>> str # still the original
'foo\n' 

So try something like this :

CODE --> REPL

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 = ln_f.rstrip()
        print (ln_f+st)
        new_Batch.write("NET USE \\\\"+ln_f+st+"/u:Some Stuff Here\n")

mfile.close()
new_Batch.close() 

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

Feherke.
feherke.github.io

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

(OP)
It works great! I just starting coding in Python and missing a few things here and there. Thank you for your help dude! bigsmile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close