test Blog
Happy living

python目录与文件操作(助记)

老鼠 posted @ 2009年4月23日 02:21 in Python with tags python 文件操作 , 2961 阅读

来自:http://hi.baidu.com/javalang/blog/item/6ce3293f866596ee55e72334.html
os和os.path模块
os.listdir(dirname):列出dirname下的目录和文件
os.getcwd():获得当前工作目录
os.curdir:返回但前目录('.')
os.chdir(dirname):改变工作目录到dirname

os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
os.path.isfile(name):判断name是不是一个文件,不存在name也返回false
os.path.exists(name):判断是否存在文件或目录name
os.path.getsize(name):获得文件大小,如果name是目录返回0L
os.path.abspath(name):获得绝对路径
os.path.normpath(path):规范path字符串形式
os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
os.path.splitext():分离文件名与扩展名
os.path.join(path,name):连接目录与文件名或目录
os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路径

>>> import os
>>> os.getcwd()
'C:\\Python25'

>>> os.chdir(r'C:\temp')
>>> os.getcwd()
'C:\\temp'

>>> os.listdir('.')
['temp.txt', 'test.py', 'testdir', 'tt']
>>> os.listdir(os.curdir)
['temp.txt', 'test.py', 'testdir', 'tt']

>>> os.path.getsize('test.py')
38L
>>> os.path.isdir('tt')
True
>>> os.path.getsize('tt')
0L

>>> os.path.abspath('tt')
'c:\\temp\\tt'
>>> os.path.abspath('test.py')
'c:\\temp\\test.py'
>>> os.path.abspath('.')
'c:\\temp'
>>>
>>> os.path.split(r'.\tt')
('.', 'tt')
>>> os.path.split(r'c:\temp\test.py')
('c:\\temp', 'test.py')
>>> os.path.split(r'c:\temp\test.dpy')
('c:\\temp', 'test.dpy'


>>> os.path.splitext(r'c:\temp\test.py')
('c:\\temp\\test', '.py')
>>> os.path.splitext(r'c:\temp\tst.py')
('c:\\temp\\tst', '.py')
>>>
>>> os.path.basename(r'c:\temp\tst.py')
'tst.py'
>>> os.path.dirname(r'c:\temp\tst.py')
'c:\\temp'
>>>

herb.apcfss.in/login 说:
2022年11月04日 21:04

Parties involved in their development include the Institute of Electronics and Governance, Treasuries and Accounts departments of Andhra Pradesh and last but not least the Andhra Pradesh technology and Services Limited. With those parties involved, herb.apcfss.in/login the operational efficiency they exhibit does not come as a surprise. To get started with the Treasury Cyber ​​bills, you need to login to the AP ddoreq. Here is a quick guide on how to go about it.

jio balance check 说:
2022年12月18日 20:59

Reliance Jio is India’s most popular telecom service company. Jio offers many options for checking balance and validity. Jio provided USSD codes for each and every mobile process to make it easy. jio balance check code You may check your JIO primary balance information will be shown on your phone’s screen. You can also check your JIO balance, Data Usage.

SBI ATM Pin Generati 说:
2023年1月25日 16:38

State Bank of India is the most popular and trusted Bank of India, which has over crores of active customers. State Bank of India brings various features in their banking system, which allows their customers to have access at their fingertips. SBI ATM Pin Generation By Sms Earlier the debit card PIN generation was always a hassle, as the customer had to wait for the PIN through courier. In present days, there are multiple ways by which one can get their Debit card PIN generated.

12th Question Paper 说:
2023年2月16日 21:30

12th Question Paper 2024 and Board Model Paper 2024 The Board of Secondary Education has declared the Board Model Paper 2024 of 2nd Year Intermediate Class. 12th Question Paper 2024 If you are also one of the enrolling students of this board and waiting for 2nd Year Intermediate Model Paper 2024. Important Question Paper 2024 then take the patient for some time and check the final passing list through the link which is mentioned at the bottom of the page.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter