Làm cách nào để lưu tệp văn bản trong thư mục trong python?
Bạn sẽ cần sửa đổi đường dẫn để phản ánh vị trí lưu trữ tệp văn bản trên máy tính của mình Bước 3. Liệt kê tất cả các tệp văn bản trong một thư mục bằng PythonĐể liệt kê tất cả các tệp văn bản trong một thư mục bằng Python, bạn sẽ cần nhập các gói glob và os Sau đó, bạn có thể sử dụng mẫu sau để liệt kê các tệp văn bản của mình import glob import os os.chdir(r'directory where the files are located') my_files = glob.glob('*.txt') print(my_files) Và đối với ví dụ của chúng tôi, đây là mã Python hoàn chỉnh để liệt kê các tệp văn bản import glob import os os.chdir(r'C:\Users\Ron\Desktop\Test') my_files = glob.glob('*.txt') print(my_files) Chạy mã (được điều chỉnh theo đường dẫn của bạn) và bạn sẽ thấy danh sách các tệp văn bản
Đừng quên đặt “r” trước đường dẫn để tránh lỗi sau trong Python (lỗi unicode) codec 'unicodeescape' không thể giải mã byte ở vị trí 2-3. rút ngắn \UXXXXXXX thoát Bản tóm tắt. trong hướng dẫn này, bạn sẽ học cách tạo một tệp văn bản mới trong Python bằng cách sử dụng hàm 2Có thể bạn quan tâmSử dụng hàm open() để tạo một tệp văn bản mớiĐể tạo một tệp văn bản mới, bạn sử dụng hàm 2. Hàm 2 có nhiều tham số. Tuy nhiên, chúng tôi sẽ tập trung vào hai tham số đầu tiên
Trong cú pháp này, tham số 0 chỉ định đường dẫn đến tệp văn bản mà bạn muốn tạo Để tạo một tệp văn bản mới, bạn sử dụng một trong các chế độ sau
Ví dụ: phần sau tạo một tệp mới có tên 6 và viết một số văn bản vào đó
Tập lệnh này tạo một tệp có tên 6 trong cùng thư mục chứa tệp tập lệnh. Nếu bạn muốn tạo một tệp trong một thư mục được chỉ định e. g. , 8, bạn cần đảm bảo thư mục 9 tồn tại trước khi tạo file. Nếu không, bạn sẽ nhận được một ngoại lệ. Ví dụ import glob import os os.chdir(r'C:\Users\Ron\Desktop\Test') my_files = glob.glob('*.txt') print(my_files)3 Lỗi import glob import os os.chdir(r'C:\Users\Ron\Desktop\Test') my_files = glob.glob('*.txt') print(my_files)4 Trong ví dụ này, Python đưa ra một ngoại lệ vì thư mục 9 không tồn tại. Do đó, nó không thể tạo tệp 6 trong thư mục đó. Để khắc phục sự cố, trước tiên bạn cần tạo thư mục 9 rồi tạo tệp 6 trong thư mục đó Ngoài ra, bạn có thể xử lý ngoại lệ bằng câu lệnh try-ngoại trừ như sau import glob import os os.chdir(r'C:\Users\Ron\Desktop\Test') my_files = glob.glob('*.txt') print(my_files)9 đầu ra 0Nếu bạn không muốn tạo một tệp văn bản mới trong trường hợp nó đã tồn tại, bạn có thể sử dụng chế độ 3 khi gọi hàm 2 |
Bài Viết Liên Quan
Hướng dẫn async python
Phần I của bài viết: https://viblo.asia/p/python-sleep-how-to-add-time-delays-to-your-code-part-i-translated-6J3ZgP6glmBPhần II của bài ...
How do you write data into a text file in python?
Python File HandlingIn Python, there is no need for importing external library to read and write files. Python provides an inbuilt function for creating, writing, and reading files.To open a file, ...
Giờ làm việc vietinbank 2023
Nắm được lịch nghỉ Tết của các ngân hàng áp dụng trên toàn hệ thống giúp khách hàng chủ động sắp xếp công việc giao dịch cho phù hợp với lịch làm ...
Hướng dẫn javascript challenges
IntroductionIf you are familiar with the DC comic universe, then you know Superman. He indeed stands tall amongst everyone. That is, in the context of the DC universe. It is in this light that I ...
How do you render html content in react?
Reacts goal is in many ways to render HTML in a web page.React renders HTML to the web page by using a function called ReactDOM.render().The Render FunctionThe ReactDOM.render() function takes two ...
Hướng dẫn label for=html
Trang chủTham khảoTag htmlĐịnh nghĩa và sử dụngTag định nghĩa nhãn cho thành phần .Tag không hiển thị bất cứ gì đặc biệt cho người dùng, tuy nhiên nó cung ...
Hướng dẫn login page html
Ngày hôm nay chúng ta sẽ cùng nhau đi vào tìm hiểu cách tạo form login được xây dựng bằng HTML CSS, Javascript với thiết kế và hiệu ứng đẹp mắt . Mong bài ...
Hướng dẫn dùng matlab percentile python
Tổng quanGần đây trong công việc của mình, tôi đã viết lại các thuật toán được phát triển trong MatLab sang Python , một số hàm không đơn giản để thích ...
Program to find xor of two numbers in python
Given two integers, find XOR of them without using the XOR operator, i.e., without using ^ in C/C++.Examples : Input: x = 1, y = 2 Output: 3 Input: x = 3, y = 5 Output: 6A Simple Solution is to ...
Hướng dẫn dùng python quiz python
Nội dung chínhThông tin về ứng dụng nàyAn toàn dữ liệuThông tin liên hệ của nhà phát triểnCác mục khác của Danijel PavlekCác ứng dụng tương tựLập trình ...
What does change () do in python?
Change Item ValueTo change the value of a specific item, refer to the index number:ExampleChange the second item: thislist = [apple, banana, cherry] thislist[1] = blackcurrant print(thislist) ...
Hướng dẫn mysql sum column
Hàm SUM() dùng để lấy giá trị tổng của một cột.Cú phápSELECT SUM(column_name) FORM table_name;Một số ví dụChúng ta có một bảng sinh_vien như sau:(Bạn có thể ...
300 tên hay cho bé gái 2023
Các bố mẹ đang muốn tìm tên con trai hay, ý nghĩa, hợp phong thủy và mang lại nhiều may mắn thịnh vượng cho tiểu bảo bối của mình thì hay tham khảo 300+ cái ...
For vs while loop python
Yes, there is a huge difference between while and for.The for statement iterates through a collection or iterable object or generator function.The while statement simply loops until a condition is ...
How to add 1 month in given date in php?
Lets say I have a date in the following format: 2010-12-11 (year-mon-day)With PHP, I want to increment the date by one month, and I want the year to be automatically incremented, if necessary (i.e. ...
How do you filter a list based on a condition in python?
How can you filter a list in Python using an arbitrary condition? The most Pythonic and most performant way is to use list comprehension [x for x in list if condition] to filter all elements from a ...
How do you print a range of palindrome numbers in python?
View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticleGiven a range of numbers, print all palindromes in the given range. For example if the given range is ...
What is timespan in javascript?
Here a .NET C# similar implementation of a timespan class that supports days, hours, minutes and seconds. This implementation also supports negative timespans.const MILLIS_PER_SECOND = 1000; const ...
How do you scale in python?
Scale FeaturesWhen your data has different values, and even different measurement units, it can be difficult to compare them. What is kilograms compared to meters? Or altitude compared to time?The ...
Stdclass to array php json
To answer the actual question:Why does PHP turn the JSON Object into a class?Take a closer look at the output of the encoded JSON, Ive extended the example the OP is giving a little bit:$array = ...