How to create a new directory in python-How to create a new directory in python

Time: 2024-12-04Source: Huajun Software TutorialAuthor: Qingchen

Python is a general-purpose computer programming language and a very beneficial tool for programmers. It allows programmers to write code quickly and has a very simple and clear syntax, which makes it ideal for completing various high-level tasks. Python can run on almost all operating systems, including Windows, macOS, Linux, etc., making it one of the first choices for cross-platform development. Next, let the editor of Huajun explain to you how to create a new directory in python and how to create a new directory in python!

first step

Start PyCharm software. Open the Python project in which you wish to create the new directory.

Python, Python latest download

Step 2

In the project view, find the folder where you want your new Python files to be placed. Right-click the folder and select "New" > "Python File". In the pop-up dialog box, enter the file name (for example, test.py), and click "OK". Double-click on the newly created test.py file to open it for editing.

Python, Python latest download

Step 3

In the editing area of the test.py file, enter import os to import the os module. Next, enter os.mkdir('NewDir') to create a new directory named NewDir. Note that NewDir here is the path relative to the current working directory. If you want to create the directory at the root of your project, you may need to use a full path or a relative path (e.g. ../NewDir, but this depends on your current working directory).

Python, Python latest download

Step 4

In the editing area of the test.py file, right-click the mouse. In the menu that pops up, select "Run 'test'" or a similar option to run your Python script.

Python, Python latest download

Step 5

After running the script, return to PyCharm's project view. Refresh the project view (usually this can be done by clicking the refresh button at the top of the project view). You should be able to see the newly created NewDir directory at the specified location.

Python, Python latest download

The above is how to create a new directory in python and the method of creating a new directory in python compiled by Huajun editor for you. I hope it can help you!

Related articles更多>>

Popular recommendations