I created a text file containing multiple path and now I want to change the path of my working directory on the basis of that txt file .I want to access all the path in text file one by one and changing the path of directory with automation.
C:\Users\ndcna\OneDrive\Desktop\python\pdfconverter12\sample\
C:\Users\ndcna\OneDrive\Desktop\python\pdfconverter12\sample\
C:\Users\ndcna\OneDrive\Desktop\python\pdfconverter12\sample\
C:\Users\ndcna\OneDrive\Desktop\python\pdfconverter12\sample\expo\
C:\Users\ndcna\OneDrive\Desktop\python\pdfconverter12\sample\expo\
so for creating txt file i used this python script
import os
target=input("Enter the directory path you want to search: - ")
pdf_file=open("file_path.txt","w")
for root, dirnames,files in os.walk(target):
for x in files:
if x.endswith(".pdf"):
pdf_file.write(root+ x +"\n")
#for changing the current direcotry
file_pat=open("file_path.txt","r")
data = file_pat.read()
data_into_list = data.split("\n")
file_pat.close()
##print(data_into_list)
##in this for loop path will change into current directory
for file in data_into_list:
path = file
os.chdir(path)
print(os.getcwd())
The above code will target those files having .pdf extension and create a file named file_path.txt having the path of pdf files showing in above file containing multiple path and now i want to make a loop which go to every line of that .txt file and change the directory with the specific path in that line one by one.
example:-
If first path in the text is
C:\Users\ndcna\OneDrive\Desktop\python\pdfconverter12\sample\
Then the working directory must be the same and so on
The second for loop (for file in data_into_list:) is working fine on one directory but it cannot go to the next file
example:- above file_path.txt have 5 path 3 are from same (C:\Users\ndcna\OneDrive\Desktop\python\pdfconverter12\sample) and 2 are from (C:\Users\ndcna\OneDrive\Desktop\python\pdfconverter12\sample\expo) so the code is working on first 3 paths but it fails on the next 2 paths (C:\Users\ndcna\OneDrive\Desktop\python\pdfconverter12\sample\expo)