Python current file path and folder delete operations

Source: Internet
Author: User
Tags pyspark

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.