python not3_Python Note3 (I/O)

Labels: Python, Keyboard Input, Files

Keyboard Input

raw_input

raw_input([prompt]): reads one line from standard input and returns it as a string. For example:

Enter your input: Hello Python

Received input is : Hello Python

input

input([prompt]): assumes the input is a valid Python expression and returns the evaluated result to you. For example:

Enter your input: [x*5 for x in range(2,10,2)]

Recieved input is : [10, 20, 30, 40]

Files

file object

Attribute

Description

file.closed

Returns true if file is closed, false otherwise.

file.mode

Returns access mode with which file was opened.

file.name

Returns name of the file.

file.softspace

Returns false if space explicitly required with print, true otherwise.

open function

Creates a file object, which would be utilized to call other support methods associated with it.

file object = open(file_name [, access_mode][, buffering])

file_name: The file_name argument is a string value that contains the name of the file that you want to access.

access_mode: The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r).

buffering: If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line buffering is performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior).

close function

A file object flushes any unwritten information and closes the file object.

fileObject.close();

write function

The write() method writes any string to an open file.

The write() method does not add a newline character ('\n') to the end of the string

Syntax

fileObject.write(string);

Example

# Open a file

fo = open("foo.txt", "wb")

fo.write( "Python is a great language.\nYeah its great!!\n");

# Close opend file

fo.close()

read function

The read() method reads a string from an open file.

Syntax

fileObject.read([count]);

count is the number of bytes to be read from the opened file. This method starts reading from the beginning of the file and if count is missing, then it tries to read as much as possible, maybe until the end of file.

File position

tell(): returns the current position within the file.

seek(offset[, from]): changes the current file position. The offset argument indicates the number of bytes to be moved. The from argument specifies the reference position from where the bytes are to be moved.

Example

# Open a file

fo = open("foo.txt", "r+")

str = fo.read(10);

print "Read String is : ", str

# Check current position

position = fo.tell();

print "Current file position : ", position

# Reposition pointer at the beginning once again

position = fo.seek(0, 0);

str = fo.read(10);

print "Again read String is : ", str

# Close opend file

fo.close()

rename() function

Needed to import Python os module.

import os

rename() takes two arguments, the current filename and the new filename.

Syntax

os.rename(current_file_name, new_file_name)

Example

import os

# Rename a file from test1.txt to test2.txt

os.rename( "test1.txt", "test2.txt" )

remove() function

Needed to import Python os module.

remove() deletes files by supplying the name of the file to be deleted as the argument.

Syntax

os.remove(file_name)

Example

import os

# Delete file test2.txt

os.remove("text2.txt")

Dictionaries

Method

Syntax

Description

mkdir()

os.mkdir("newdir")

create directories in the current directory.

chdir()

os.chdir("newdir")

change the current directory.

getcwd()

os.getcwd()

displays the current working directory.

rmdir()

os.rmdir('dirname')

deletes the directory.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值