CAT equivalent in python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CAT equivalent in python
# 1  
Old 05-10-2010
CAT equivalent in python

cat is such a simple and useful command in UNIX. I was wonder if python had any equivalent.

More specifically, I have a .txt file that I would like displayed from a python script.

Is there a similar command besides "print?" When I try to use the print command, the text is formatted differently (one long string with "\n")

Thanks!
# 2  
Old 05-10-2010
I'm a totally py n00b, but I was curios and after googling for a few minutes I found out following code will do the thing:
Code:
$ cat readfile.py
#!/usr/local/bin/python

f = open("data.dat", "r")
text = f.read()
print text
f.close()
$

You probably used readlines which puts the output in an array or something

Last edited by pseudocoder; 05-11-2010 at 12:30 AM.. Reason: added f.close() ;)
# 3  
Old 05-10-2010
thanks. I did find a similar code, but yours is definitely easier to read. And you're right, I was using .read/.readlines which printed everything out in an array.

Thank you so much for your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

Python Cat

Hi, Below is the Python Script am trying to execute: #!/usr/bin/python import os import sys import subprocess import time def mpg(node, sitename): os.system('/usr/local/bin/expect /gsn/pdp/pdp_expect ' + node + ' > /tmp/' + node + '.temp') date = os.popen("cat /tmp/" + node +... (9 Replies)
Discussion started by: infinitydon
9 Replies

4. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

5. SuSE

"ssh suse-server 'python -V' > python-version.out" not redirecting

Okay, so I have had this problem on openSUSE, and Debian systems now and I am hoping for a little help. I think it has something to do with Python but I couldn't find a proper Python area here. I am trying to redirect the output of "ssh suse-server 'python -V'" to a file. It seems that no matter... (3 Replies)
Discussion started by: Druonysus
3 Replies

6. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

7. Shell Programming and Scripting

for i in `cat myname.txt` && for y in `cat yourname.txt`

cat myname.txt John Doe I John Doe II John Doe III ----------------------------------------------------------------------- for i in `cat myname.txt` do echo This is my name: $i >> thi.is.my.name.txt done ----------------------------------------------------------------------- cat... (1 Reply)
Discussion started by: danimad
1 Replies

8. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies
Login or Register to Ask a Question