Hướng dẫn how do you create .txt file in a specific folder in python? - làm cách nào để tạo tệp .txt trong một thư mục cụ thể trong python?

Làm thế nào để bạn nói với Python nơi lưu tệp văn bản?

Ví dụ: máy tính của tôi đang chạy tệp Python khỏi máy tính để bàn của tôi. Tôi muốn nó lưu tất cả các tệp văn bản trong thư mục tài liệu của tôi, không phải trên máy tính để bàn của tôi. Làm thế nào để tôi làm điều đó trong một kịch bản như thế này?

name_of_file = raw_input("What is the name of the file: ")
completeName = name_of_file + ".txt"
#Alter this line in any shape or form it is up to you.
file1 = open(completeName , "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()

Hướng dẫn how do you create .txt file in a specific folder in python? - làm cách nào để tạo tệp .txt trong một thư mục cụ thể trong python?

Đã hỏi ngày 6 tháng 11 năm 2011 lúc 0:01Nov 6, 2011 at 0:01

Chỉ cần sử dụng một đường dẫn tuyệt đối khi mở FileHandle để viết.

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()

Bạn có thể tùy chọn kết hợp điều này với

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
2 như được mô tả trong câu trả lời của Bryan để tự động lấy đường dẫn của thư mục tài liệu của người dùng. Chúc mừng!

Hướng dẫn how do you create .txt file in a specific folder in python? - làm cách nào để tạo tệp .txt trong một thư mục cụ thể trong python?

Mehdi Nellen

7.9564 Huy hiệu vàng32 Huy hiệu bạc48 Huy hiệu đồng4 gold badges32 silver badges48 bronze badges

Đã trả lời ngày 6 tháng 11 năm 2011 lúc 0:02Nov 6, 2011 at 0:02

AcornacornAcorn

47.6K26 Huy hiệu vàng129 Huy hiệu bạc171 Huy hiệu đồng26 gold badges129 silver badges171 bronze badges

0

Sử dụng OS.Path.Join để kết hợp đường dẫn đến thư mục

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
3 với
import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
4 (tên tệp?) Được cung cấp bởi người dùng.

import os
with open(os.path.join('/path/to/Documents',completeName), "w") as file1:
    toFile = raw_input("Write what you want into the field")
    file1.write(toFile)

Nếu bạn muốn thư mục

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
3 có liên quan đến thư mục nhà của người dùng, bạn có thể sử dụng một cái gì đó như:

os.path.join(os.path.expanduser('~'),'Documents',completeName)

Những người khác đã đề xuất sử dụng

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
6. Lưu ý rằng
import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
6 không giải quyết
import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
8 cho thư mục nhà của người dùng:

In [10]: cd /tmp
/tmp

In [11]: os.path.abspath("~")
Out[11]: '/tmp/~'

Hướng dẫn how do you create .txt file in a specific folder in python? - làm cách nào để tạo tệp .txt trong một thư mục cụ thể trong python?

Đã trả lời ngày 6 tháng 11 năm 2011 lúc 0:04Nov 6, 2011 at 0:04

UnutbuUnutbuunutbu

803K173 Huy hiệu vàng1728 Huy hiệu bạc1628 Huy hiệu đồng173 gold badges1728 silver badges1628 bronze badges

Một bản cập nhật nhỏ cho điều này.

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
9 được đổi tên thành
import os
with open(os.path.join('/path/to/Documents',completeName), "w") as file1:
    toFile = raw_input("Write what you want into the field")
    file1.write(toFile)
0 trong Python 3.

Ghi chú phát hành Python 3

Hướng dẫn how do you create .txt file in a specific folder in python? - làm cách nào để tạo tệp .txt trong một thư mục cụ thể trong python?

xrisk

3.67021 Huy hiệu bạc42 Huy hiệu đồng21 silver badges42 bronze badges

Đã trả lời ngày 20 tháng 9 năm 2018 lúc 12:26Sep 20, 2018 at 12:26

der_radlerder_radlerder_radler

5094 Huy hiệu bạc15 Huy hiệu Đồng4 silver badges15 bronze badges

Một cách đơn giản khác mà không sử dụng hệ điều hành nhập khẩu là,

outFileName="F:\\folder\\folder\\filename.txt"
outFile=open(outFileName, "w")
outFile.write("""Hello my name is ABCD""")
outFile.close()

Đã trả lời ngày 30 tháng 12 năm 2018 lúc 1:48Dec 30, 2018 at 1:48

Hướng dẫn how do you create .txt file in a specific folder in python? - làm cách nào để tạo tệp .txt trong một thư mục cụ thể trong python?

Paul Thomaspaul ThomasPaul Thomas

4677 Huy hiệu bạc15 Huy hiệu Đồng7 silver badges15 bronze badges

Nếu bạn muốn lưu một tệp vào một thư mục cụ thể và tên tệp thì đây là một số ví dụ đơn giản. Nó cũng kiểm tra xem thư mục có hoặc chưa được tạo không.

import os.path
directory = './html/'
filename = "file.html"
file_path = os.path.join(directory, filename)
if not os.path.isdir(directory):
    os.mkdir(directory)
file = open(file_path, "w")
file.write(html)
file.close()

Hy vọng điều này sẽ giúp bạn!

Đã trả lời ngày 3 tháng 12 năm 2019 lúc 3:45Dec 3, 2019 at 3:45

Hướng dẫn how do you create .txt file in a specific folder in python? - làm cách nào để tạo tệp .txt trong một thư mục cụ thể trong python?

AsherasherAsher

2.5186 Huy hiệu vàng26 Huy hiệu bạc40 Huy hiệu đồng6 gold badges26 silver badges40 bronze badges

Sử dụng một chuỗi tuyệt đối hoặc tương đối làm tên tệp.

name_of_file = input("What is the name of the file: ")
completeName = '/home/user/Documents'+ name_of_file + ".txt"
file1 = open(completeName , "w")
toFile = input("Write what you want into the field")
file1.write(toFile)
file1.close()

Đã trả lời ngày 11 tháng 10 năm 2018 lúc 10:20Oct 11, 2018 at 10:20

Hướng dẫn how do you create .txt file in a specific folder in python? - làm cách nào để tạo tệp .txt trong một thư mục cụ thể trong python?

1

Chỉ cần cung cấp đường dẫn mong muốn của bạn nếu tệp không tồn tại sớm hơn;

        from os.path import abspath
        with open ('C:\\Users\\Admin\\Desktop\\results.txt', mode = 'w') as final1:
            print(final1.write('This is my new file.'))

        print(f'Text has been processed and saved at {abspath(final1.name)}')

Đầu ra sẽ là:

Text has been processed and saved at C:\Users\Admin\Desktop\results.txt

Đã trả lời ngày 30 tháng 6 năm 2020 lúc 4:03Jun 30, 2020 at 4:03

Hướng dẫn how do you create .txt file in a specific folder in python? - làm cách nào để tạo tệp .txt trong một thư mục cụ thể trong python?

Nếu bạn muốn lưu tệp hoặc một số khác trong thư mục, bạn có thể sử dụng mô -đun

import os
with open(os.path.join('/path/to/Documents',completeName), "w") as file1:
    toFile = raw_input("Write what you want into the field")
    file1.write(toFile)
1 từ thư viện Python cơ sở.

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
0

Bạn có thể làm điều đó cho một số danh sách của một số tệp hoặc trong các truy vấn Django:

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
1

Tất cả các tập tin được chọn của bạn sẽ được đặt trong thư mục có hướng.

Đã trả lời ngày 17 tháng 3 năm 2021 lúc 15:52Mar 17, 2021 at 15:52

Làm thế nào để bạn tạo một tệp văn bản trong một thư mục cụ thể trong Python?

Cách viết một tập tin vào một thư mục cụ thể trong Python..
save_path = '/home'.
file_name = "test.txt".
hoàn thành tên = hệ điều hành. đường dẫn. tham gia (save_path, file_name).
print(completeName).
File1 = Open (hoàn thành tên, "W").
FILE1. Viết ("Thông tin tệp").
FILE1. gần().

Làm cách nào để tạo một tệp văn bản trong một thư mục?

Windows 10 Microsoft cung cấp một cách tạo tệp văn bản mới, trống bằng menu nhấp chuột phải trong File Explorer.Mở tệp Explorer và điều hướng đến thư mục nơi bạn muốn tạo tệp văn bản.Nhấp chuột phải vào thư mục và truy cập tài liệu văn bản mới>.Tệp văn bản được cung cấp một tên mặc định, tài liệu văn bản mới.Right-click in the folder and go to New > Text Document. The text file is given a default name, New Text Document.

Làm thế nào để bạn tạo một tệp TXT trong Python?

Cách tạo tập tin trong Python.Trong Python, bạn sử dụng hàm Open () với một trong các tùy chọn sau - "X" hoặc "W" - để tạo một tệp mới: "X" - Tạo: Lệnh này sẽ tạo một tệp mới khi và chỉ khi cóKhông có tệp đã tồn tại với tên đó nếu không nó sẽ trả về lỗi.use the open() function with one of the following options – "x" or "w" – to create a new file: "x" – Create: this command will create a new file if and only if there is no file already in existence with that name or else it will return an error.

Làm cách nào để lưu một tệp vào một thư mục cụ thể trong Python?

Cách lưu tệp văn bản vào một thư mục khác trong Python..
Phương pháp 1: Sử dụng đường dẫn tuyệt đối ..
Phương pháp 2: Sử dụng hệ điều hành.đường dẫn().
Phương pháp 3: Sử dụng SHOTIL.Sao chép ().
Phương pháp 4: Sử dụng đường dẫn ..