Python study notes [08] to read and write files

8.1 files and paths

8.1.1 on the back slash and OS X on Windows and Linux forward slash

Different systems solve the problem by the path separator function os.path.join

>>> import os
>>> os.path.join('usr','bin','spam')
'usr/bin/spam'

8.1.2 The current working directory

>>> Import OS
 >>> The os.getcwd () Gets the current working directory #
 ' / the Users / wooluwalker / Documents ' 
>>> the os.chdir ( ' / the Users / wooluwalker ' ) # change the current path
 >>> OS. getcwd ()
 ' / the Users / wooluwalker '

8.1.4 with os.makedirs () Create a new folder

>>> import os
>>> os.makedirs('/Users/wooluwalker/Desktop/testFile/pytest/makedirs_test')
>>> os.chdir('/Users/wooluwalker/Desktop/testFile/pytest/makedirs_test')
>>> os.getcwd()
'/Users/wooluwalker/Desktop/testFile/pytest/makedirs_test'

This command creates a multiple nested directories, test1 / test2 / test3 are before nonexistent directory.

 

8.1.5 os.path module

os module os.path module is a module, the module can be introduced using os

These include functions related to the file name, file path

8.1.6 handling absolute and relative paths

os.path.abspath ( '.'): Returns the current working directory absolute path

os.path.isabs (path): determine whether a path is the absolute path

os.path.relpath (path, start): Returns a string from the start of the path to the opposite path, path 

 

Guess you like

Origin www.cnblogs.com/wooluwalker/p/11627140.html