c python shell文件读写

文章目录

linux

#include <stdio.h>
//#include <sys/types.h>
//#include <sys/stat.h>
#include <fcntl.h>      //headfile of open
#include <errno.h>      //headfile of errno
#include <string.h>     //headfile of sterror
#include <unistd.h>     //headfile of read

void readFile(){
        char filepath[] = "/home/zijing/readfile.txt";
        int     fd = open(filepath, O_RDONLY);
        if(fd == -1){
                printf("read error, %s -->  %s\n",filepath, strerror(errno));

                return;
        }
//      printf("success fd = %d\n", fd);
        char buf[100];
        memset(buf,0,sizeof(buf));

        while(read(fd, buf, sizeof(buf) - 1) > 0){
                printf("%s\n", buf);
                memset(buf, 0, sizeof(buf));
        }
        printf("%s\n",buf);
        close(fd);
}

void writeFile(){
        char filepath[] = "/home/zijing/writefile.txt";
        int     fd = open(filepath, O_RDWR | O_APPEND);

        if (fd == -1){
                printf("write error, %s -- > %s\n", filepath, strerror(errno));
                return;
        }

//      printf("success fd = %d\n", fd);
        char buf[100];
        memset(buf, 0, sizeof(buf));
        strcpy(buf, "hello linux from c\n");
        write(fd,buf,strlen(buf));
        close(fd);
}

int main(int arg, char *args[]) {
        readFile();
        writeFile();
        return 0;
}

python

def readfileOnce():
        file = open('/home/zijing/readfile.txt', encoding='utf-8')
        data = file.read()
        file.close()
        print(data,end='')                      #end line without '\n'


def readfileBylist():
        file = open('/home/zijing/readfile.txt','r',encoding='utf-8')
        for line in file.readlines():
                print(line,end = '')    #end line without '\n'
        file.close()

def readfileBylines():
        # high efficent
        file = open('/home/zijing/readfile.txt','r',encoding='utf-8')
        for line in file:
                print(line,end = '')    #end line without '\n'
        file.close()


def writefile():
        file = open('/home/zijing/writefile.txt','a',encoding='utf-8')
        file.write("hello linux from python\n")
        file.close()


readfileOnce()
readfileBylist()
readfileBylines()
writefile()

shell

#!/bin/bash
#sign:          2017/9/17       zijing
#funtion:       file opreate of read and file


FILEPATH="/home/zijing/readfile.txt"
FILEWRITEPATH="/home/zijing/writefile.txt"

function readfile(){
        cat $FILEPATH | while read line; do
        echo $line;
        done
}

function writefile(){
        echo "hello linux from shell" >> $FILEWRITEPATH
}

readfile
writefile
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值