Python Forum
opening files and output of parsing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
opening files and output of parsing
#1
So I have the following text file named test.txt containing the following lines:

This is line 1.
This is line 2.
This is line 3.

The following code is used to parse the file:

testfile = open(r"C:\Users\Mohammad\Documents\test.txt")
result = [line for line in testfile if "2" in line]
print(result)
Output:
['This is line 2.\n']

How do I lose the \n. part in the output?
Reply
#2
Usually you'll want to rstrip(), which by default removes any whitespace from the right end.

result = [line.rstrip() for line in testfile if "2" in line]
leodavinci1990 likes this post
Reply
#3
(Oct-01-2020, 02:27 AM)bowlofred Wrote: Usually you'll want to rstrip(), which by default removes any whitespace from the right end.

result = [line.rstrip() for line in testfile if "2" in line]
Reply
#4
(Oct-12-2020, 04:21 AM)leodavinci1990 Wrote:
(Oct-01-2020, 02:27 AM)bowlofred Wrote: Usually you'll want to rstrip(), which by default removes any whitespace from the right end.

result = [line.rstrip() for line in testfile if "2" in line]

Follow-up questions:

1)How do I, instead of having to give the full path of the file in the open function, just use test.txt  ? In other words, how do I change the directory to the current directory where the Python file is currently being run from?
2) Why do I run into an error when opening the file using the following code:

f=open("C:\Users\Mohammad\Documents\test.txt", "r")
or
f=open("C:\Users\Mohammad\Documents\test.txt")

and why did putting an r before the filename in open(r"C:\Users\Mohammad\Documents\test.txt")
solve it?
Reply
#5
1.  Go ahead and use "test.txt" and see if it works.  The current directory is the current directory of the shell that started the program (may or may not be where the python file is.)  If you run ".\foo.py", then the current directory is the same.  But if you run "otherdir\foo.py", then the current directory is above otherdir, not inside it.  You can change the current directory within the program, but you can't guarantee what it will be ahead of time.  

import os
print(os.getcwd())
C:\Temp>python subdir\printdir.py
C:\Temp


2.  Some python strings use backslash escape sequences.  As an example a newline is "\n" and a tab character is "\t".  The "r-string" turns off the backslash specials and just makes it a normal character.  When dealing with windows paths (and all the backslashes they have), turning off the "special" ones is easier than doubling all the backslashes.

>>> print("c:\new folder\timesheets")
c:
ew folder       imesheets
>>> print(r"c:\new folder\timesheets")
c:\new folder\timesheets
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Phyton Opening files on windows C: pc EddieG 3 994 Mar-29-2023, 03:19 PM
Last Post: buran
  merge two csv files into output.csv using Subprocess mg24 9 1,792 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Increment text files output and limit contains Kaminsky 1 3,206 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  Opening and closing Mac applications and files Nickd12 5 5,726 Sep-05-2020, 04:39 AM
Last Post: perfringo
  Parsing Xml files >3gb using lxml iterparse Prit_Modi 2 2,354 May-16-2020, 06:53 AM
Last Post: Prit_Modi
  PYHTON not opening files profficial 11 13,612 Feb-15-2020, 09:06 AM
Last Post: bharathendra
  Four text files input combinations into an output file mhyga 2 2,221 Jul-28-2019, 06:52 PM
Last Post: ThomasL
  Parsing Attached .MSG Files with Python3 ericl42 1 3,683 Apr-12-2019, 06:28 PM
Last Post: ericl42
  parsing local xml files to csv erdem_ustunmu 8 5,169 Feb-27-2019, 12:37 PM
Last Post: erdem_ustunmu
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,838 Dec-30-2018, 04:07 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020