I want to collect the data from the first line in each .txt file, all the files are in the same folder. I then want to save it in the variable: (addPlayer) This will be needed to add items to a pqt5 list Folder directory: C:UsersHendreOneDriveDocumentsCourseWorkProjectPlayers
Code:
JavaScript
x
4
1
for files in Players:
2
addPlayer = #first line of file
3
self.playerlist.addItem(addPlayer)
4
Advertisement
Answer
I think that will help you
JavaScript
1
16
16
1
import os
2
folder_path = "path to folder with files"
3
#folder_path = os.getcwd() #put your script in folder with files, then it return it's path
4
5
# that returns all .txt files pathes
6
Players = [file for file in os.listdir(folder_path) if file.endswith('.txt')]
7
8
addPlayer = list()
9
10
for files in Players:
11
with open(files, 'r') as f:
12
line_data = f.readline()
13
#if in files are more then one line
14
line_data = line_data.replace("n", "")
15
addPlayer.append(line_data)
16