Hướng dẫn can python create new folders? - python có thể tạo thư mục mới không?

Đã hỏi 13 năm, 3 tháng trước 13 years, 3 months ago

Đã xem 586k lần 586k times

Tôi muốn đặt thông tin đầu ra của chương trình của mình vào một thư mục. Nếu thư mục được đưa ra không tồn tại, thì chương trình sẽ tạo một thư mục mới với tên thư mục như được đưa ra trong chương trình. Điều này có thể? Nếu có, xin vui lòng cho tôi biết làm thế nào.

Giả sử tôi đã cung cấp đường dẫn thư mục như thư mục

mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
1 và
mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
2 không tồn tại thì chương trình sẽ tạo thư mục
mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
2 và nên đặt thông tin đầu ra vào thư mục
mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
2.

Hướng dẫn can python create new folders? - python có thể tạo thư mục mới không?

Gấp đôi AA

5.61716 Huy hiệu vàng43 Huy hiệu bạc56 Huy hiệu Đồng16 gold badges43 silver badges56 bronze badges

Hỏi ngày 13 tháng 8 năm 2009 lúc 20:33Aug 13, 2009 at 20:33

1

Bạn có thể tạo một thư mục với OS.MADEDIRS () và sử dụng Os.Path.Exists () để xem nó đã tồn tại:
and use os.path.exists() to see if it already exists:

newpath = r'C:\Program Files\arbitrary' 
if not os.path.exists(newpath):
    os.makedirs(newpath)

Nếu bạn đang cố gắng tạo trình cài đặt: Windows Installer sẽ làm rất nhiều việc cho bạn.

Hướng dẫn can python create new folders? - python có thể tạo thư mục mới không?

Pixdigit

802 huy hiệu bạc11 huy hiệu đồng2 silver badges11 bronze badges

Đã trả lời ngày 13 tháng 8 năm 2009 lúc 20:43Aug 13, 2009 at 20:43

Mcandremcandremcandre

21.6K19 Huy hiệu vàng84 Huy hiệu bạc145 Huy hiệu đồng19 gold badges84 silver badges145 bronze badges

7

Bạn đã thử OS.MKDIR chưa?

Bạn cũng có thể thử đoạn mã mã nhỏ này:

mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)

Makedirs tạo ra nhiều cấp độ thư mục, nếu cần.

Hướng dẫn can python create new folders? - python có thể tạo thư mục mới không?

Đã trả lời ngày 13 tháng 8 năm 2009 lúc 20:39Aug 13, 2009 at 20:39

Hướng dẫn can python create new folders? - python có thể tạo thư mục mới không?

JuergenjuergenJuergen

Huy hiệu vàng 12K738 Huy hiệu bạc55 Huy hiệu đồng7 gold badges38 silver badges55 bronze badges

Bạn có thể muốn OS.MADEDIRS vì nó cũng sẽ tạo ra các thư mục trung gian, nếu cần.

import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)

Đã trả lời ngày 13 tháng 8 năm 2009 lúc 20:46Aug 13, 2009 at 20:46

Alex Martellialex MartelliAlex Martelli

830K164 Huy hiệu vàng1205 Huy hiệu bạc1385 Huy hiệu Đồng164 gold badges1205 silver badges1385 bronze badges

2

Tóm tắt: Trong hướng dẫn này, bạn sẽ học cách thao tác các thư mục trong Python bằng mô -đun

mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
5.: in this tutorial, you’ll learn how to manipulate directories in Python using the
mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
5 module.

Nhận thư mục làm việc hiện tại

Thư mục làm việc hiện tại là thư mục mà tập lệnh Python đang chạy. Để có được thư mục làm việc hiện tại, bạn sử dụng

mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
6 như sau:

import os cwd = os.getcwd() print(cwd)

Code language: JavaScript (javascript)

Để thay đổi thư mục làm việc hiện tại, bạn sử dụng chức năng

mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
7:

import os os.chdir('/script') cwd = os.getcwd() print(cwd)

Code language: JavaScript (javascript)

Tham gia và chia một con đường

Để làm cho một chương trình hoạt động trên các nền tảng bao gồm Windows, Linux và MacOS, bạn cần sử dụng các đường dẫn thư mục và tệp độc lập với nền tảng.

Python cung cấp cho bạn một mô hình con

mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
8 chứa một số chức năng và hằng số hữu ích để tham gia và phân chia đường dẫn.

Hàm

mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
9 kết hợp các thành phần đường dẫn với nhau và trả về một đường dẫn với bộ phân cách đường dẫn tương ứng. Ví dụ: nó sử dụng dấu gạch chéo ngược (
import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
0) trên Windows và Slash chuyển tiếp (
import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
1) trên MacOS hoặc Linux.

Hàm

import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
2 chia một đường dẫn vào các thành phần mà không có dấu phân cách đường dẫn. Ở đây, một ví dụ về việc sử dụng các hàm
mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
9 và
import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
2:

import os fp = os.path.join('temp', 'python') print(fp) # temp\python (on Windows) pc = os.path.split(fp) print(pc) # ('temp', 'python')

Code language: PHP (php)

Kiểm tra nếu một đường dẫn là một thư mục

Để kiểm tra xem một đường dẫn có tồn tại không và là một thư mục, bạn có thể sử dụng các chức năng

import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
5 và
import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
6. Ví dụ:

import os dir = os.path.join("C:\\", "temp") print(dir) if os.path.exists(dir) or os.path.isdir(dir): print(f'The {dir} is a directory')

Code language: PHP (php)

Tạo một thư mục

Để tạo một thư mục mới, bạn sử dụng hàm

import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
7. Và bạn phải luôn luôn kiểm tra xem một thư mục tồn tại trước khi tạo một thư mục mới.

Ví dụ sau đây tạo ra một thư mục mới gọi là

import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
8 trong thư mục
import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
9.

import os dir = os.path.join("C:\\", "temp", "python") if not os.path.exists(dir): os.mkdir(dir)

Code language: JavaScript (javascript)

Đổi tên một thư mục

Để đổi tên thư mục, bạn sử dụng hàm

import os cwd = os.getcwd() print(cwd)

Code language: JavaScript (javascript)
0:

import os oldpath = os.path.join("C:\\", "temp", "python") newpath = os.path.join("C:\\", "temp", "python3") if os.path.exists(oldpath) and not os.path.exists(newpath): os.rename(oldpath, newpath) print("'{0}' was renamed to '{1}'".format(oldpath, newpath))

Code language: JavaScript (javascript)

Xóa một thư mục

Để xóa một thư mục, bạn sử dụng chức năng

import os cwd = os.getcwd() print(cwd)

Code language: JavaScript (javascript)
1 như sau:

import os dir = os.path.join("C:\\","temp","python") if os.path.exists(dir): os.rmdir(dir) print(dir + ' is removed.')

Code language: JavaScript (javascript)

Đi qua một thư mục một cách đệ quy

Hàm

import os cwd = os.getcwd() print(cwd)

Code language: JavaScript (javascript)
2 cho phép bạn đi qua một thư mục đệ quy. Hàm

import os cwd = os.getcwd() print(cwd)

Code language: JavaScript (javascript)
2 trả về thư mục gốc, các thư mục con và tệp.

Ví dụ sau đây cho thấy cách in tất cả các tệp và thư mục trong thư mục

import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
9:

mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)
0

Bản tóm tắt

  • Sử dụng chức năng
    mypath = ...
    if not os.path.isdir(mypath):
       os.makedirs(mypath)
    
    6 để có được thư mục làm việc hiện tại.
  • Sử dụng chức năng
    mypath = ...
    if not os.path.isdir(mypath):
       os.makedirs(mypath)
    
    7 để thay đổi thư mục làm việc hiện tại thành một thư mục mới.
  • Sử dụng chức năng
    import os
    
    #dir is not keyword
    def makemydir(whatever):
      try:
        os.makedirs(whatever)
      except OSError:
        pass
      # let exception propagate if we just can't
      # cd into the specified directory
      os.chdir(whatever)
    
    7 để tạo thư mục mới.
  • Sử dụng chức năng

    import os cwd = os.getcwd() print(cwd)

    Code language: JavaScript (javascript)
    0 để đổi tên một thư mục.
  • Sử dụng chức năng

    import os cwd = os.getcwd() print(cwd)

    Code language: JavaScript (javascript)
    1 để xóa một thư mục.
  • Sử dụng hàm

    import os cwd = os.getcwd() print(cwd)

    Code language: JavaScript (javascript)
    2 để liệt kê nội dung của thư mục.

Bạn có thấy hướng dẫn này hữu ích không?

Python mở có tạo ra các thư mục không?

Mô -đun HĐH của Python bao gồm các chức năng để tạo và xóa các thư mục (thư mục), truy xuất nội dung của chúng, thay đổi và xác định thư mục hiện tại, v.v.Để giao diện với hệ điều hành cơ bản, trước tiên bạn phải nhập mô -đun OS., retrieving their contents, altering and identifying the current directory, and more. To interface with the underlying operating system, you must first import the os module.

Các thư mục được gọi là trong Python là gì?

Nhưng đừng nhầm lẫn;Một từ điển chỉ đơn giản là những gì bạn gọi là một thư mục.Trong hướng dẫn thư mục Python này, chúng tôi sẽ nhập mô -đun HĐH để có thể truy cập các phương pháp chúng tôi sẽ áp dụng.a dictionary is simply what you call a folder. In this Python Directory tutorial, we will import the OS module to be able to access the methods we will apply.