Hướng dẫn write file in folder python - ghi tệp vào thư mục 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ỏ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!

Mehdi Nellen

7.8864 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.4K26 Huy hiệu vàng128 Huy hiệu bạc171 Huy hiệu đồng26 gold badges128 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/~'

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

UnutbuUnutbuunutbu

798K171 Huy hiệu vàng1722 Huy hiệu bạc1624 Huy hiệu đồng171 gold badges1722 silver badges1624 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

xrisk

3.63020 Huy hiệu bạc41 Huy hiệu đồng20 silver badges41 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

5074 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

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

AsherasherAsher

2.5086 huy hiệu vàng26 Huy hiệu bạc39 Huy hiệu đồng6 gold badges26 silver badges39 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

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

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 cách nào để lưu tệp vào thư mục trong Python?

Lưu tệp lưu vào một thư mục khác trong câu trả lời của mã Python..
Nhập hệ điều hành. đường dẫn..
save_path = 'C:/ví dụ/'.
name_of_file = Raw_input ["Tên của tệp là gì:"].
hoàn thành tên = hệ điều hành. đường dẫn. tham gia [save_path, name_of_file+". txt"].

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

Để ghi vào tệp văn bản bằng Python, bạn làm theo các bước sau: Đầu tiên, hãy mở tệp văn bản để ghi [hoặc nối] bằng hàm Open []..Third, Đóng tệp bằng phương thức đóng [].open the text file for writing [or append] using the open[] function. Second, write to the text file using the write[] or writelines[] method. Third, close the file using the close[] method.

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

Các bước cần thiết để lưu tệp vào một vị trí tiêu chuẩn ...
Khởi chạy hộp thoại Lưu tệp.Trong menu Tệp, chọn mục Save As Menu ..
Đặt tên cho tập tin.Mở thư mục chứa tệp mong muốn.....
Chọn thư mục mong muốn để lưu tệp.....
Chỉ định loại định dạng tệp ..
Nhấp vào nút Lưu ..

Làm thế nào để bạn tạo một tệp và viết nó bằng Python?

Làm thế nào để tạo một tệp văn bản trong Python..
Bước 1] Mở tệp .txt f = Mở ["Guru99.txt", "W+"] ....
Bước 2] Nhập dữ liệu vào tệp cho I trong phạm vi [10]: f.write ["Đây là dòng % d \ r \ n" % [i+1]] ....
Bước 3] Đóng phiên bản tệp f.close [] ....
Bước 1] F = Mở ["Guru99.txt", "A+"].

Bài Viết Liên Quan

Chủ Đề