Where do I store text files for Python - python

Which folder location do I store text files on my computer for python to access? I'm trying to open a file called word.txt with the command fin = open('words.txt').

You need to use the full path to the file.
with open('/path/to/words.txt', 'r') as handle:
print handle.read()
Otherwise, it will be using your current directory.
import os
# Print your current directory
print os.path.abspath(os.curdir)

Related

how can i store only pdf file or filename from folder containing another files with different extensions (.doc, .txt, .html) in variable using python?

Let consider a folders(Mandar and html) on your desktop. Now paste any pdf file and named it 'dell' in 'html' folder and create demo.py file in 'Mandar' folder. Now create some txt files(2-4) as your wish so that 'html' folder contains some txt files and only one pdf file.
import os
import PyPDF2 # install via 'pip install PyPDF2'
# Put location of your pdf file i.e. dell.pdf in 'location' variable
location = "C:/Users/Desktop/html/"
n = "dell.pdf"
path = os.path.join(location, n)
reader = PyPDF2.PdfReader(path)
pages = len(reader.pages)
print(f"The no. of pages in {n} is {pages}.")
Now run program and you see that
''The no. of pages in dell.pdf is NUM.'' //NUM is no. of pages of your pdf
Now let consider 'html' folder always contain only one pdf file with any name maybe dell, maybe ecc, maybe any name. I want that variable 'n' stores this one pdf file in itself as input so that the program will run and display same result with different pdf file name and Num.
Give glob in the standard library a shot. It'll get you a list of all the matching PDF files in that directory.
import os
import PyPDF2
...
import glob
Location='C:/Users/Desktop/html/'
candidates = glob.glob(os.path.join(Location, '*.pdf'))
if len(candidates) == 0:
raise Exception('No PDFs found')
File=open(candidates[0],'rb')
...
You're looking for globbing. You can do that with pathlib:
from pathlib import Path
root = Path(location)
pdf_files = root.glob("*.pdf")

Open a file in python from 2 directory back

I want to read a file from 2 folders back..
with open('../../test.txt', 'r') as file:
lines = file.readlines()
file.close()
I want to read from ../../ two folders back. but not work..
How i can do that ?
Opening files in python is relative to the current working directory. This means you would have to change cd to the directory where this python file is located.
If you want a more robust solution:
To be able to run this from any directory, there is a simple trick:
import os
PATH = os.path.join(os.path.dirname(__file__), '../../test.txt')
with open(PATH, 'r') as file:
lines = file.readlines()
file.close()

Using Python, able to iterate files from another directory but unable to read contents

I need to iterate through all the .txt files inside another directory from current directory and read the contents.
Am able to iterate thw filenames, but when tried to read the contents, the output is blabk while there is data in the files.
To do that here is an example of how to iterate on every text file in the current directory
# Import the module OS
import os
# Choose Current Directory
directory = os.getcwd()
# Iterate over every file in the directory
for filename in os.listdir(directory):
# get the full path by concatenating the file name with the directory
path = directory + "\\" + filename
# Check if this file is a text file
if path.endswith(".txt"):
# Open the text files in the path
text_file = open(path, 'r')
# Read and print the contents of these files (Including the Python Script)
print(text_file.read())
You can change the current directory with any directory you need.
If you have a problem with other Directories try using double backslashes in the URL like this
directory = 'C:\\Users\\USER\\Desktop\\Text Files'
for file in files :
print(file)
print("printing success file")
k = open(extracted_files_path+file,"r")
print(extracted_files_path + file)
print("ending printing success file")
print(k.read())
print(type(file))
Output:
printing success file ending printing success file
//below is blank, whereas the file contents are expected// <class
'str'>
The above is the code and the actual output I'm getting for the question - raven.

Filter Directory using Regex and output filtered files to another directory

I am simply trying to create a python 3 program that runs through all .sql files in a specific directory and then apply my regex that adds ; after a certain instance and write the changes made to the file to a separate directory with their respective file names as the same.
So, if I had file1.sql and file2.sql in "/home/files" directory, after I run the program, the output should write those two files to "/home/new_files" without changes the content of the original files.
Here is my code:
import glob
import re
folder_path = "/home/files/d_d"
file_pattern = "/*sql"
folder_contents = glob.glob(folder_path + file_pattern)
for file in folder_contents:
print("Checking", file)
for file in folder_contents:
read_file = open(file, 'rt',encoding='latin-1').read()
#words=read_file.split()
with open(read_file,"w") as output:
output.write(re.sub(r'(TBLPROPERTIES \(.*?\))', r'\1;', f, flags=re.DOTALL))
I receive an error of File name too long:"CREATE EXTERNAL TABLe" and also I am not too sure where I would put my output path (/home/files/new_dd)in my code.
Any ideas or suggestions?
With read_file = open(file, 'rt',encoding='latin-1').read() the whole content of the file was being used as the file descriptor. The code provided here iterate over the files names found with glob.glob pattern open to read, process data, and open to write (assuming that a folder newfile_sqls already exist,
if not, an error would rise FileNotFoundError: [Errno 2] No such file or directory).
import glob
import os
import re
folder_path = "original_sqls"
#original_sqls\file1.sql, original_sqls\file2.sql, original_sqls\file3.sql
file_pattern = "*sql"
# new/modified files folder
output_path = "newfile_sqls"
folder_contents = glob.glob(os.path.join(folder_path,file_pattern))
# iterate over file names
for file_ in [os.path.basename(f) for f in folder_contents]:
# open to read
with open(os.path.join(folder_path,file_), "r") as inputf:
read_file = inputf.read()
# use variable 'read_file' here
tmp = re.sub(r'(TBLPROPERTIES \(.*?\))', r'\1;', read_file, flags=re.DOTALL)
# open to write to (previouly created) new folder
with open(os.path.join(output_path,file_), "w") as output:
output.writelines(tmp)

Opening a file and creating a new file in the same folder

def Function222(inF):
inF = open("C:\\Users\\Dell\\Desktop\\FF1\\txttt.txt")
outputF=open("output.txt", "w")
lines=inF.readlines()
for line in lines:
outputF.write('\n')
outputF.write(line*4)
inF.close()
outputF.close()
I need to create a new file called outputF and it should show up in the same folder that the inF is in, the problem is that it doesn't appear in the folder and I searched for the file on my computer but didn't find it
Get the Path:
import os
path= os.path.abspath("C:/example/cwd/mydir/myfile.txt")
open new file in path and write to it
Because the current working directory isn't the directory of the input file. Use os.getcwd() to get the current working directory, if it doesnt't match the directory of the input file, then you need to change your working directory first:
import os
def Function222(inF):
inF = open("C:\\Users\\Dell\\Desktop\\FF1\\txttt.txt")
#change the working directory
os.chdir("C:\\Users\\Dell\\Desktop\\FF1")
outputF=open("output.txt", "w")
lines=inF.readlines()
for line in lines:
outputF.write('\n')
outputF.write(line*4)
inF.close()
outputF.close()

Categories