Objective:
Python file operations are partially different from Java. Due to the needs of the project, the recent use of Python module development encountered some common file operations to surf the internet for some, feeling a lot of opinions. So, with its own usage scenario, a Python code is posted for subsequent review.
Prepare a test file "C://test/a.txt".
#Encoding:utf-8ImportOSImportShutilif __name__=='__main__': Print "Current workspace directory------------>" PrintOs.pathPrintOS.GETCWD ()PrintOs.getcwdu ()Print Print "file (s) Operation------------>"path1="C:/test/a.txt"path2="c:/test" Printos.path.exists (path1)Printos.path.exists (path2)#Os.removedirs (r "C://test") # Only empty folders can be deletedShutil.rmtree (path2)Printos.path.exists (path1)PrintOs.path.exists (path2)
The console output is as follows:
Current Workspace directory------------>
<module ' Ntpath ' from ' E:\Python27\lib\ntpath.pyc ' >
D:\PyCharm\cjkj-cloud-pyspark-ml
D:\PyCharm\cjkj-cloud-pyspark-ml
File (s) Operation------------>
True
True
False
False
Python current file path and folder delete operations