Đã 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.
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.
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.
Đã trả lời ngày 13 tháng 8 năm 2009 lúc 20:39Aug 13, 2009 at 20:39
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
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]
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:Code language: JavaScript [javascript]
import os cwd = os.getcwd[] print[cwd]
Để 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:Code language: JavaScript [javascript]
import os os.chdir['/script'] cwd = os.getcwd[] print[cwd]
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:Code language: PHP [php]
import os fp = os.path.join['temp', 'python'] print[fp] # temp\python [on Windows] pc = os.path.split[fp] print[pc] # ['temp', 'python']
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ụ:Code language: PHP [php]
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']
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.Code language: JavaScript [javascript]
import os dir = os.path.join["C:\\", "temp", "python"] if not os.path.exists[dir]: os.mkdir[dir]
Đổi tên một thư mục
Để đổi tên thư mục, bạn sử dụng hàm
0:Code language: JavaScript [javascript]
import os cwd = os.getcwd[] print[cwd]
Code language: JavaScript [javascript]
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]]
Xóa một thư mục
Để xóa một thư mục, bạn sử dụng chức năng
1 như sau:Code language: JavaScript [javascript]
import os cwd = os.getcwd[] print[cwd]
Code language: JavaScript [javascript]
import os dir = os.path.join["C:\\","temp","python"] if os.path.exists[dir]: os.rmdir[dir] print[dir + ' is removed.']
Đi qua một thư mục một cách đệ quy
Hàm
2 cho phép bạn đi qua một thư mục đệ quy. HàmCode language: JavaScript [javascript]
import os cwd = os.getcwd[] print[cwd]
2 trả về thư mục gốc, các thư mục con và tệp.Code language: JavaScript [javascript]
import os cwd = os.getcwd[] print[cwd]
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]
0Bản tóm tắt
- Sử dụng chức năng
6 để có được thư mục làm việc hiện tại.mypath = ... if not os.path.isdir[mypath]: os.makedirs[mypath]
- Sử dụng chức năng
7 để thay đổi thư mục làm việc hiện tại thành một thư mục mới.mypath = ... if not os.path.isdir[mypath]: os.makedirs[mypath]
- Sử dụng chức năng
7 để tạo thư mục mới.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]
- Sử dụng chức năng
0 để đổi tên một thư mục.
Code language: JavaScript [javascript]import os cwd = os.getcwd[] print[cwd]
- Sử dụng chức năng
1 để xóa một thư mục.
Code language: JavaScript [javascript]import os cwd = os.getcwd[] print[cwd]
- Sử dụng hàm
2 để liệt kê nội dung của thư mục.
Code language: JavaScript [javascript]import os cwd = os.getcwd[] print[cwd]
Bạn có thấy hướng dẫn này hữu ích không?