Python-Import files
Every file has own path to access.
e.g.
C:\Users\asweigart\Documents\project.docx
When comes to OS X and Linux, we use / to separate.
在 OS X 上,它们表示为新的文件夹,在/Volumes 文件夹下。在 Linux 上,它们表示为新的
文件夹,在/mnt(”mount”)文件夹下。同时也要注意,虽然文件夹名称和文件名在
Windows 和 OS X 上是不区分大小写的,但在 Linux 上是区分大小写的。
import files
os.path.join()
1 | import os |
1 | myFiles = ['accounts.txt', 'details.csv', 'invite.docx'] |
current folder:
getcwd() : check which folder is the process in
chdir() :change the current folder (the folder must exist)
1 | import os |
create new folder:os.makedirs()
1 | import os |
AP(absolute path) and relative path
processing with absolute path and relative path
os.path.abspath():show the absolute path(if something in the blanket,add it to the end of the absolute path, which means to open one folder or file in the original folder)
os.path.isabs():check wether it is absolute path
os.path.relpath(path,start):find the relative path from the start
os.path.dirname(path):return the string after the last \
os.path.split():separate the dirname and the basename by the last \
split() can also be used
1 | os.path.abspath('.') |
others:
方法 | 说明 |
---|---|
os.path.abspath(path) | 返回绝对路径 |
os.path.basename(path) | 返回文件名 |
os.path.commonprefix(list) | 返回list(多个路径)中,所有path共有的最长的路径 |
os.path.dirname(path) | 返回文件路径 |
os.path.exists(path) | 如果路径 path 存在,返回 True;如果路径 path 不存在,返回 False。 |
os.path.lexists | 路径存在则返回True,路径损坏也返回True |
os.path.expanduser(path) | 把path中包含的” |
os.path.expandvars(path) | 根据环境变量的值替换path中包含的”$name”和”${name}” |
os.path.getatime(path) | 返回最近访问时间(浮点型秒数) |
os.path.getmtime(path) | 返回最近文件修改时间 |
os.path.getctime(path) | 返回文件 path 创建时间 |
os.path.getsize(path) | 返回文件大小,如果文件不存在就返回错误 |
os.path.isabs(path) | 判断是否为绝对路径 |
os.path.isfile(path) | 判断路径是否为文件 |
os.path.isdir(path) | 判断路径是否为目录 |
os.path.islink(path) | 判断路径是否为链接 |
os.path.ismount(path) | 判断路径是否为挂载点 |
os.path.join(path1[, path2[, …]]) | 把目录和文件名合成一个路径 |
os.path.normcase(path) | 转换path的大小写和斜杠 |
os.path.normpath(path) | 规范path字符串形式 |
os.path.realpath(path) | 返回path的真实路径 |
os.path.relpath(path[, start]) | 从start开始计算相对路径 |
os.path.samefile(path1, path2) | 判断目录或文件是否相同 |
os.path.sameopenfile(fp1, fp2) | 判断fp1和fp2是否指向同一文件 |
os.path.samestat(stat1, stat2) | 判断stat tuple stat1和stat2是否指向同一个文件 |
os.path.split(path) | 把路径分割成 dirname 和 basename,返回一个元组 |
os.path.splitdrive(path) | 一般用在 windows 下,返回驱动器名和路径组成的元组 |
os.path.splitext(path) | 分割路径,返回路径名和文件扩展名的元组 |
os.path.splitunc(path) | 把路径分割为加载点与文件 |
os.path.walk(path, visit, arg) | 遍历path,进入每个目录都调用visit函数,visit函数必须有3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有文件名,args则为walk的第三个参数 |
os.path.supports_unicode_filenames | 设置是否支持unicode路径名 |
check the size and name
1 | os.path.getsize('C:\\Windows\\System32\\calc.exe') |
os.path.exists(path): check whether it exists
os.path.isfile(path): check whether it is a file
os.path.isdir(path): check whether it is a folder
1 | os.path.exists('C:\\Windows') |
read the file
open()
windows:
1 | helloFile = open('C:\\Users\\your_home_folder\\hello.txt') |
OS X
1 | helloFile = open('/Users/your_home_folder/hello.txt') |
read()
1 | helloContent = helloFile.read() |
When, in disgrace with fortune and men’s eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself and curse my fate,
1 | sonnetFile = open('sonnet29.txt') |
write()
1 | baconFile = open('bacon.txt', 'w') |
shelve
在交互环境下 under interactive environment
1 | import shelve |
1 | shelfFile = shelve.open('mydata') |
1 | shelfFile = shelve.open('mydata') |
pprint.pformat()
to show the content but not print
1 | import pprint |
import can be used toimport files
1 | import myCats |
example
You want to create 35 different quiz files for 35 students tocheck their knowledge about sates and their capitals in America. Every quiz should include 50 multiple choice questions, and they should be in different order.
1 | #! python3 |
Name:
Date:
Period:
State Capitals Quiz (Form 1)
- What is the capital of West Virginia?
A. Hartford
B. Santa Fe
C. Harrisburg
D. Charleston
- What is the capital of Colorado?
A. Raleigh
B. Harrisburg
C. Denver
D. Lincoln
… …