c和python的檔案讀寫

2021-07-29 17:44:00 字數 1029 閱讀 3172

最近在c和python之間來回使用檔案讀寫,有時候會發生混淆,在這做個總結,小記一下。

c檔案讀寫的基礎函式:

對檔案描述符操作

intopen(const char *pathname, int flags, mode_t mode);

ssize_t read(int fd, void *buf, size_t count);

ssize_t write(int fd, const void *buf, size_t count);

對檔案指標操作

file *fopen(const char *path, const char *mode);

size_t fread(void *ptr, size_t size, size_t nmemb, file *stream);

size_t fwrite(const void *ptr, size_t size, size_t nmemb, ile *stream);

檔案描述符 檔案指標轉換

int fileno(file *stream);

file *fdopen(int fd, const char *mode);

python檔案讀寫基礎函式

對檔案描述符操作

os.open(file, flags[, mode]);

os.read(fd,n)

os.write(fd, str)

對檔案指標操作

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

file.read([size])

file.write(str)

檔案描述符 檔案指標轉化

file.fileno()

os.fdopen(fd, [, mode[, bufsize]]);

檔案指標操作是高階io,有緩衝區

檔案描述符是低階io,沒有緩衝區

python檔案讀寫和整理

在windows上面,路徑的書寫使用反斜槓作為資料夾之間的分隔符 在os x或者linux上則是是用正斜槓。python上對檔案進行操作要使用到os這個模組。1.os模組和os.path模組的相關方法。使用os.path.join 函式,通過給它傳遞各資料夾的名字,會返回乙個檔案路徑的字串,並根據使...

python檔案和變數讀寫

對檔案操作首先需要找的檔案的路徑,os.path模組可以很好地對路徑進行操作問題。結構資料 變數讀寫 讀寫檔案有3個步驟 呼叫open 函式開啟檔案,返回file物件 呼叫file物件的read 或write 方法讀或寫檔案 呼叫file物件的close 方法,關閉檔案。向open 函式傳入乙個路徑...

python檔案讀寫和異常

1,文字檔案 2,二進位制檔案 print type data with open 吉多.jpg wb as fs2 fs2.write data except filenotfounderror as e print 指定的檔案無法開啟.except ioerror as e print 讀寫檔案...