I have a file called "mp3.txt" with the following content:
1.mp3
2.mp3
3.mp3
4.mp3
5.mp3
The Python code to read the "mp3.txt", and create a new file for each line:
from pathlib import Path
with open("mp3.txt", "r") as file:
for line in file.readlines():
Path(line).touch()
Result:
1.mp3? 3.mp3? 5.mp3?
2.mp3? 4.mp3?
I'm only experiencing this when I'm reading from a file. If I do the same thing from a list() instead of a file like so:
from pathlib import Path
l = ["1.mp3", "2.mp3", "3.mp3", "4.mp3", "5.mp3"]
for i in l:
Path(i).touch()
The output looks ok:
1.mp3 2.mp3 3.mp3 4.mp3 5.mp3
Yes, I did try appending the file's content into a list():
from pathlib import Path
l = []
with open("mp3.txt", "r") as file:
for line in file.readlines():
l.append(line)
for i in l:
Path(i).touch()
and the "?" is still being appended in the filename. The documentation don't have anything on this type of behavior.
Strip the strings (file names). The "new line" you have after each file name cause the ?
to be part of the file that is created by Path
from pathlib import Path
with open("mp3.txt", "r") as file:
file_names = [line.strip() for line in file.readlines()]
for file_name in file_names:
Path(file_name).touch()