Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 18th, 2012, 7:09 PM   #1
kaykav
Newbie
 
Join Date: Feb 2012
Posts: 4
Rep Power: 0 kaykav is on a distinguished road
using os.open()

HI
I've been watching you tube tutorials,and came across the usage of modules. How do I properly use os.open() ? I'm in python interpreter, imported os, and now I want to read a text file in my home folder. : >>> os.open('/home/kaykav/fileone.txt', 'r') . I looked at examples but could not understand them. Thank you...
kaykav is offline   Reply With Quote
Old Mar 18th, 2012, 8:00 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Google / Kitchener
Posts: 4,154
Rep Power: 15 Sane has a spectacular aura aboutSane has a spectacular aura about
Send a message via MSN to Sane
Re: using os.open()

A quote from the Python documentation on os.open:

Quote:
This function is intended for low-level I/O. For normal usage, use the built-in function open().
Here is a link to the correct 'open' (no os module): http://docs.python.org/library/functions.html#open

I don't know what tutorial you're using, but it sounds pretty terrible. Do some reading on the normal (non-OS) open/read/write. Feel free to ask questions about any examples that you don't understand.
__________________
PFO's Folding@Home Team | Sane's Monthly Algorithms Challenges
Rules | How to Post a Question | How to Post Code

Becoming a good programmer requires foresight of your code's execution.
Becoming an excellent programmer requires foresight of your code's modification.
Sane is offline   Reply With Quote
Old Mar 21st, 2012, 4:17 PM   #3
Purity_Lake
Hobbiest Programmer
 
Purity_Lake's Avatar
 
Join Date: May 2011
Location: Ireland
Posts: 131
Rep Power: 4 Purity_Lake is on a distinguished road
Re: using os.open()

sane is right, the open() function is the one that you should be using for reading or writing to files among other tasks that the python documentation can tell you

here is an example of the open() function in use:

# open a file and allowing you to write to file 
# (if file doesn't exist python makes one with the name you define)
file = open("hello.txt", 'w')

# you can write into this file by doing the following
file.write("Hello World")
sentence = "Hello Again World"
file.write(sentence)
userInput = raw_input("Input words or numbers:\n")
file.write(userInput)
list = ["Hello", "World", "from", "my", "list"]
for a in list:
   file .write(a)
# that is a few examples of how you can use the open() functions write method

# make sure to close file at the end of execution of your code
file.close()

# if you want to print whats in a text file you can do the follwing
file = open("new file.txt", 'r')
content = file.read()
print content
file.close()

hope these examples help u
Purity_Lake is offline   Reply With Quote
Old Mar 23rd, 2012, 7:35 AM   #4
kaykav
Newbie
 
Join Date: Feb 2012
Posts: 4
Rep Power: 0 kaykav is on a distinguished road
Re: using os.open()

OK
I'll follow-up on your advice...Thank you; we'll talk again...
kaykav is offline   Reply With Quote
Reply

Bookmarks

Tags
open()

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC