Python3教學
Python3引入什麼新的東西?
Python3環境安裝設置
Python3基礎語法
Python3變量類型
Python算術運算符實例
Python3比較運算符實例
Python3賦值運算符實例
Python3位運算符實例
Python3邏輯運算符實例
Python3成員運算符實例
Python3標識運算符實例
Python3運算符優先級示例
Python3基本運算符
Python3 if語句
Python3 if...elseif...else語句
Python3嵌套if語句
Python3決策
Python3 while循環語句
Python3 for循環語句
Python3嵌套循環
Python3 break語句
Python3 continue語句
Python3 pass語句
Python3循環
Python3 abs()函數
Python3 ceil()函數
Python3 exp()函數
Python3 fabs()函數
Python3 floor()函數
Python3 log()函數
Python3 log10()函數
Python3 max()函數
Python3 min()函數
Python3 modf()函數
Python3 pow()函數
Python3 round()函數
Python3 sqrt()函數
Python3 choice()函數
Python3 randrange()函數
Python3 random()函數
Python3 seed()函數
Python3 shuffle()函數
Python3 uniform()函數
Python3 acos()函數
Python3 asin()函數
Python3 atan()函數
Python3 atan2()函數
Python3 cos()函數
Python3 hypot()函數
Python3 sin()函數
Python3 tan()函數
Python3 degrees()函數
Python3 radians()函數
Python3數字
Python3 string.capitalize()方法
Python3 string.center()方法
Python3 string.count()方法
Python3 string.decode()方法
Python3 string.endswith()方法
Python3 string.expandtabs()方法
Python3 string.encode()方法
Python3 string.find()方法
Python3 string.index()方法
Python3 string.isalnum()方法
Python3 string.isalpha()方法
Python3 string.isdigit()方法
Python3 string.tolower()方法
Python3 string.isnumeric()方法
Python3 string.isspace()方法
Python3 string.istitle()方法
Python3 string.isupper()方法
Python3 string.join()方法
Python3 string.len()方法
Python3 string.ljust()方法
Python3 string.lower()方法
Python3 string.lstrip()方法
Python3 string.maketrans()方法
Python3 string.max()方法
Python3 string.min()方法
Python3 string.replace()方法
Python3 string.rfind()方法
Python3 string.rindex()方法
Python3 string.rjust()方法
Python3 string.rstrip()方法
Python3 string.split()方法
Python3 string.splitlines()方法
Python3 string.startswith()方法
Python3 string.strip()方法
Python3 string.swapcase()方法
Python3 string.title()方法
Python3 string.translate()方法
Python3 string.upper()方法
Python3 string.zfill()方法
Python3 string.isdecimal()方法
Python3字符串
Python3 list.cmp()方法
Python3 list.len()方法
Python3 list.max()方法
Python3 list.min()方法
Python3 list.list()方法
Python3 list.append()方法
Python3 list.count()方法
Python3 list.extend()方法
Python3 list.index()方法
Python3 list.insert()方法
Python3 list.pop()方法
Python3 list.remove()方法
Python3 list.reverse()方法
Python3 list.sort()方法
Python3列表
Python3 tuple.cmp()方法
Python3 tuple.len()方法
Python3 tuple.max()方法
Python3 tuple.min()方法
Python3 tuple.tuple()方法
Python3元組
Python3 dictionary.cmp()方法
Python3 dictionary.len()方法
Python3 dictionary.str()方法
Python3 dictionary.type()方法
Python3 dictionary.clear()方法
Python3 dictionary.copy()方法
Python3 dictionary.fromkeys()方法
Python3 dictionary.get()方法
Python3 dictionary.has_key()方法
Python3 dictionary.items()方法
Python3 dictionary.keys()方法
Python3 dictionary.setdefault()方法
Python3 dictionary.update()方法
Python3 dictionary.values()方法
Python3字典
Python3 time.altzone()方法
Python3 time.asctime()方法
Python3 time.clock()方法
Python3 time.ctime()方法
Python3 time.gmtime()方法
Python3 time.localtime()方法
Python3 time.mktime()方法
Python3 time.sleep()方法
Python3 time.strftime()方法
Python3 time.time()方法
Python3 time.tzset()方法
Python3日期和時間
Python3函數
Python3模塊
Python3 file.close()方法
Python3 file.tell()方法
Python3 file.truncate()方法
Python3 file.write()方法
Python3 file.writelines()方法
Python3 file.flush()方法
Python3 file.fileno()方法
Python3 file.isatty()方法
Python3 file.next()方法
Python3 file.read()方法
Python3 file.readline()方法
Python3 file.readlines()方法
Python3 file.seek()方法
Python3文件方法
Python3 os.access()方法
Python3 os.chdir()方法
Python3 os.chflags()方法
Python3 os.chmod()方法
Python3 os.chown()方法
Python3 os.chroot()方法
Python3 os.close()方法
Python3 os.closerange()方法
Python3 os.dup()方法
Python3 os.dup2()方法
Python3 os.fchdir()方法
Python3 os.fchmod()方法
Python3 os.fchown()方法
Python3 os.fdatasync()方法
Python3 os.fopen()方法
Python3 os.fpathconf()方法
Python3 os.fstat()方法
Python3 os.fstatvfs()方法
Python3 os.fsync()方法
Python3 os.ftruncate()方法
Python3 os.getcwd()方法
Python3 os.getcwdu()方法
Python3 os.isatty()方法
Python3 os.lchflags()方法
Python3 os.lchmod()方法
Python3 os.lchown()方法
Python3 os.link()方法
Python3 os.listdir()方法
Python3 os.lseek()方法
Python3 os.lstat()方法
Python3 os.major()方法
Python3 os.makedev()方法
Python3 os.makedirs()方法
Python3 os.minor()方法
Python3 os.mkdir()方法
Python3 os.mkfifo()方法
Python3 os.mknod()方法
Python3 os.open()方法
Python3 os.openpty()方法
Python3 os.pathconf()方法
Python3 os.pipe()方法
Python3 os.popen()方法
Python3 os.read()方法
Python3 os.readlink()方法
Python3 os.remove()方法
Python3 os.removedirs()方法
Python3 os.rename()方法
Python3 os.renames()方法
Python3 os.rmdir()方法
Python3 os.stat()方法
Python3 os.stat_float_times()方法
Python3 os.statvfs()方法
Python3 os.symlink()方法
Python3 os.tcgetpgrp()方法
Python3 os.tcsetpgrp()方法
Python3 os.tempnam()方法
Python3 os.tmpfile()方法
Python3 os.tmpnam()方法
Python3 os.ttyname()方法
Python3 os.unlink()方法
Python3 os.utime()方法
Python3 os.walk()方法
Python3 os.write()方法
Python3 os文件目錄的方法
Python3文件操作
Python3標準異常
Python3斷言
Python3異常處理

Python3 os.dup2()方法

dup2()方法複製文件描述符fd到fd2,如果必要之後就關閉它。

注意:只有在新的文件描述可用時才被分配。 在下面給出的下面的例子中,1000在可用時,1000將被分配作爲重複的fd。

語法

以下是 dup2()方法的語法:

os.dup2(fd, fd2)

參數

  • fd -- 這是要被複制的文件描述符

  • fd2 -- 這是重複的文件描述符

返回值

這個方法返回文件描述符的副本。

示例

下面的示例說明 dup2() 方法的使用。

#!/usr/bin/python3

import os, sys

Open a file

fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

Write one string using duplicate fd

line="this is test"

string needs to be converted byte object

b=str.encode(line)
os.write(fd, b)

Now duplicate this file descriptor as 1000

fd2 = 1000
os.dup2(fd, fd2);

Now read this file from the beginning using fd2.

os.lseek(fd2, 0, 0)
line = os.read(fd2, 100)
str=line.decode()
print ("Read String is : ", str)

Close opened file

os.closerange( fd,fd2 )

print ("Closed the file successfully!!")

當我們運行上面的程序,它會產生以下結果:

Read String is : This is test
Closed the file successfully!!