0

Hi stackoverflow community,

Situation,
I'm trying to run this converter found from here,
However what I want is for it to read an array of file path from a text file and convert them.

Reason being, these file path are filtered manually, so I don't have to convert unnecessary files. There are a large amount of unnecessary files in the folder.

How can I go about with this? Thank you.

enter image description here

haste556
  • 21
  • 5

2 Answers2

1
with open("file_path",'r') as file_content:
    content=file_content.read()
content=content.split('\n')

You can read the data of the file using the method above, Then covert the data of file into a list(or any other iteratable data type) so that we can use it with for loop.I used content=content.split('\n') to split the data of content by '\n' (Every time you press enter key, a new line character '\n' is sended), you can use any other character to split.

for i in content:
   # the code you want to execute

Note

Some useful links:

Faraaz Kurawle
  • 1,085
  • 6
  • 24
0

By looking at your situation, I guess this is what you want (to only convert certain file in a directory), in which you don't need an extra '.txt' file to process:

import os

for f in os.listdir(path):
    if f.startswith("Prelim") and f.endswith(".doc"):
        convert(f)
        
        

But if for some reason you want to stick with the ".txt" processing, this may help:

with open("list.txt") as f:
    lines = f.readlines()
    for line in lines:
        convert(line)
        
ytung-dev
  • 872
  • 1
  • 2
  • 12