建立檔案追加檔案

2022-05-17 05:03:26 字數 1297 閱讀 2378

投稿:junjie 字型:[增加

這篇文章主要介紹了python建立檔案和追加檔案內容例項,本文同時給出了把標準輸出定向到檔案例項,需要的朋友可以參考下

一、用python建立乙個新檔案,內容是從0到9的整數, 每個數字佔一行:

複製****如下:

#python 

>>>f=open('f.txt','w')    # r唯讀,w可寫,a追加

>>>for i in range(0,10):f.write(str(i)+'\n')

.  .  .

>>> f.close()

二、檔案內容追加,從0到9的10個隨機整數:

複製****如下:

#python

>>>import random

>>>f=open('f.txt','a')

>>>for i in range(0,10):f.write(str(random.randint(0,9)))

.  .  .

>>>f.write('\n')

>>>f.close()

三、檔案內容追加,從0到9的隨機整數, 10個數字一行,共10行:

複製****如下:

#python

>>> import random

>>> f=open('f.txt','a')

>>> for i in range(0,10):

.  .  .     for i in range(0,10):f.write(str(random.randint(0,9)))  

.  .  .     f.write('\n')     

.  .  .

>>> f.close()

四、把標準輸出定向到檔案:

複製****如下:

#python

>>> import sys

>>> sys.stdout = open("stdout.txt", "w")

例子:檢視22埠情況,並將結果寫入a.txt

複製****如下:

#!/usr/bin/python

#coding=utf-8

import os

import time

import sys

f=open('a.txt','a')

f.write(os.popen('netstat -nltp | grep 22').read())

f.close()

IO 建立檔案

public class demo03 catch exception e public static void test3 throws ioexception,interruptedexception 刪除檔案 boolean flag src.delete system.out.println...

建立檔案鏈結

硬連線 硬連線指通過索引節點來進行連線。在linux的檔案系統中,儲存在磁碟分割槽中的檔案不管是什麼型別都給它分配乙個編號,稱為索引節點號 inode index 在linux中,多個檔名指向同一索引節點是存在的。一般這種連線就是硬連線。硬連線的作用是允許乙個檔案擁有多個有效路徑名,這樣使用者就可以...

建立檔案練習

test 用命令和正規表示式按照要求建立檔案 用一條命令建立12個檔案westos classx linuxy x的數值範圍1 2,y的數值範圍為1 6 這些檔案都包含在root使用者桌面的study目錄中 用一條命令建立8個檔案redhat versionx x的範圍是1 8 redhat ver...