美文网首页
Python Note3 (I/O)

Python Note3 (I/O)

作者: qin7zhen | 来源:发表于2017-02-23 17:18 被阅读12次

Labels: Python, Keyboard Input, Files

Ref: Python Files I/O https://www.tutorialspoint.com/python/python_files_io.htm


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.

相关文章

  • Python Note3 (I/O)

    Labels: Python, Keyboard Input, Files Ref: Python Files I...

  • I\O文件的读写

    Python文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档。 input函数 i...

  • Python_IO编程

    本篇文章将介绍python里面的I/O编程。更多内容请参考:python学习指南 I/O编程 读写文件时最常见的I...

  • Python入门系列(七)——I/O

    目录:一、界面I/O二、文件I/O 一、界面I/O 我也不知道这么称呼是否贴切,python首先要提到的就是用户交...

  • python

    一、python基本变量 二、函数 三、文件I/O

  • python i/o操作

    python中i/o相关的模块是os,os.path模块,shutil模块是对os模块的扩展,也包含了很多实用的函...

  • Python异步I/O

    CPU的速度远远快于磁盘、网络等IO。在一个线程中,CPU执行代码的速度极快,然而,一旦遇到IO操作,如读写文件、...

  • Python 文件I/O

    1.1文件操作介绍I/O IO 操作是相对于内存进行 in 输入,读入。从硬盘中读到内存 out 输出。从内存写到...

  • python 文件I/O

    Python文件I/O ·File对象方法: file对象提供了操作文件的一系列方法。 ·OS对象方法:提供了处理...

  • python 常用I/O

    CSV import csv csvFile =open(csv_File_path, 'wt', newline...

网友评论

      本文标题:Python Note3 (I/O)

      本文链接:https://www.haomeiwen.com/subject/ysjbwttx.html