Python: "FileNotFoundError" Despite being able to print such files - python-3.x

I'm working on a Python3 script where the code walks through directories and sub-directories to pull out all gzipped warc files.
I'd like to also add that the files are not in my home directory
file_path = os.path.join('/nappa7/pip73/Service')
walk_file(parallel_bulk, file_path)
Perhaps python is not looking where i think it's looking, nevertheless, here is my walk_file functions:
def walk_file(bulk, file_path):
warc = warcat.model.WARC()
try:
for (file_path,dirs,files) in os.walk(file_path):
for filenames in files:
if filenames.endswith('.warc.gz'):
warc.load(filenames)
except ValueError:
pass
When I replace the warc.load(filenames) with a print statement like so:
if filenames.endswith('.warc.gz'):
print(filenames)
The filenames are printed out onto the console as expected. Therefore, It leads me to believe that python was able to succesfully locate all warc.gz files. However, when i try the warc.load(filenames), i get:
FileNotFoundError: [Errno 2] No such file or directory: 'Sample.warc.gz'
I can certainly use some guidance.
Thank you.
So for anyone else who has a similar issue:
changing the code to this worked:
warc.load(os.path.join(file_path, filenames))

You need to use os.path.join(file_path, filenames) instead of just filenames.
Otherwise the operating system will look for the file in the current directory instead of file_path.
(And why is filenames plural when it refers to a single filename?)

Related

How to create a file in python having name = ".gitignore"

I was trying to create a file without a name in python (only filetype)
I tried this -
open(".gitignore","w+").close()
But it does not work.
edit - it does work real issue is in getting file through glob.glob
classify_folder_name = #path of the folder which contain .gitignore file
rel_paths = glob.glob(classify_folder_name + '/**', recursive=True)
for local_file in rel_paths:
print(local_file)
it does not print .gitignore file.
Any help will be appreciated.
Note -: don't want to use os.listdir()
There are few things that you might check:
files with dot at the beginning are hidden so whatever OS you are using, make sure you have hidden files visibility enabled
It might be saved in different directory
open(".gitignore","w+").close()
It would be better if you do this:
To create a file:
with open('.gitignore', 'w') as fp:
pass

WinError2 keeps popping up with this python 3.7.3 script to delete files in a file tree without having to scroll through them

I am REALLY (2 days) new to all of this. I am trying to delete a bunch of files in a folder in my external HD with a python 3.7.3 script but an error keeps popping up.
Firstly, this code works fine and finds the folders:
import os
for folderName, subfolders, filenames in os.walk("D:\Practice"):
for filename in filenames:
if filename.endswith('practice.docx'):
#os.unlink(filename)
print(filename)
But then when I remove the print(filename) the remove the hash, the folders can't be deleted with the following error popping up:
import os
for folderName, subfolders, filenames in os.walk("D:\Practice"):
for filename in filenames:
if filename.endswith('practice.docx'):
os.unlink(filename)
os.unlink(filename) FileNotFoundError: [WinError 2] The system cannot find the file specified: 'rootpractice.docx'
The 'rootpractice' doc is clearly recognised but won't be deleted.
Does anyone know how I can solve this? Any help for this total beginner is much appreciated.
os.unlink and similar other methods which require file paths expect either a link relative to current folder in which your script is running (which you can find out with os.getcwd() ), or the full path.
When you are iterating with os.walk, you are only passing the filename and not the full path. Try this:
import os
for folderName, subfolders, filenames in os.walk("D:\Practice"):
for filename in filenames:
if filename.endswith('practice.docx'):
full_path = os.path.join(folderName, filename)
print("About to delete the file: {}".format(full_path))
os.unlink(full_path)

Can`t use the files inside my subdirectories

I`m creating a program that can read certain data from some txt files, the problem comes when I try to use the files inside subdirectories (the subdirectories are inside the main directory of the program. I'm using a for the option to find all the files and then create a new file with the info that I found. The main problem is that I can't read those files.
I tried using a for a function that creates a list of directories, files and roots, this works fine, but in the moment of running the file it says "it cannot be found txt file". The if not condition is made so the program excludes all.DS_Store files. I think the problem could be the way I open the file but im not sure
for root, directories, filenames in os.walk("Files_to_Insert"):
if not (filenames[-1] == ".DS_Store"):
lastFile = filenames[-1]
print lastFile
with open (lastFile, 'rt') as myfile:
IOError: [Errno 2] No such file or directory: txt
The mistake happens in the with open because it can`t find the file.
When I print I get all the txt files, but I can,t use them in the "with open"
A typical os.walk I use goes like this:
import os
for root, directories, filenames in os.walk("."):
for f in filenames:
if f.endswith(".DS_Store"):
continue
print(os.path.abspath(f))
with open (os.path.abspath(f), 'rt') as myfile:
I solve it by giving the path and the text file in separate strings:
for root, directories, filenames in os.walk("Files_to_Insert"):
if not(filenames[-1] == ".DS_Store"):
lastFile = filenames[-1]
# print (lastFile)
with open(str(root) + '/' + lastFile,'rt') as myfile:

I am getting FileNotFoundError: [Errno 2] No such file or directory: 'dna.txt' on Pycharm? Where do I put file to read?

I am trying to open 2 text files on PyCharm but it says file not found. Where do I put the files so they can be found?
I tried moving the files to the folder where all my PyCharm projects are kept but it didn't work.
dna = open('dna.txt', 'r')
dna.close()
dna_results = open("dnaresults.txt", "w")
dna_results.close()
Expected: I don't know honestly, for the text file to open on PyCharm so I can read it?
Actual: FileNotFoundError: [Errno 2] No such file or directory: 'dna.txt'
You need to pass the full path of the file in order to open it, for e.g. if the file is located at /home/john/dna.txt, you need to do.
dna = open('/home/john/dna.txt', 'r')
dna.close()
You can put your file anywhere you want, but you always need to pass the full path of the file in order to access it, otherwise the python interpreter doesn't know where to find it.
As an additional tidbit, if the dna.txt is located in the same folder as where the script is located, dna = open('dna.txt', 'r') will work

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\mswitajski\\Desktop\\alice.txt'

I'm trying to read in a text file to work with Word Clouds. Here is the syntax I'm trying:
# Read the whole text.
text = open(r'C:\Users\mswitajski\Desktop\alice.txt').read()
But I keep getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\mswitajski\\Desktop\\alice.txt'
I've triple checked the file name, tried reading it as a raw file, changed the slashes and everything but I continue to get the same error.
Well, if someone reaches up to here and still could not find the solution then here is the more pythonic way of doing the absolute path in windows.
Instead of using:
text = open(r'C:\Users\mswitajski\Desktop\alice.txt').read()
use os.sep, in conjunction of os.path.join like the following:
import os
text = open(os.path.join('C:', os.sep, 'Users', 'mswitajski', 'Desktop', 'alice.txt')).read()
Try changin the path to this"
'C:\Users\mswitajski\Desktop/alice.txt'
Sometimes windows won't find/recognize the file path when the file is specified like this
'C:\Users\mswitajski\Desktop\alice.txt'
In the answer it shows up as only one \ but you still need 2 like your previous path. The only difference is the last slash /. Hope that works.
At your text raw file (alice.txt) try delete the .txt.
The file probably is named alice.txt.txt
I face the same issue and solve it by deleted the .txt.
I had to use double slashes instead of one, because python interpreted it as a escape sequence. My final string was:
C:\\Users\\ArpitChinmay's\\AppData\\Roaming\\Code\\User\\globalStorage\\moocfi.test-my-
code\\tmcdata\\TMC workspace\\Exercises\\hy\\hy-data-analysis-with-python-
2020\\part02-e04_word_frequencies\\src\\alice.txt
However, it worked this way too,
C:\\Users\\Arpit Chinmay's\\AppData\\Roaming\\Code\\User\\globalStorage\\moocfi.test-
my-code\\tmcdata\\TMC workspace\\Exercises\\hy\\hy-data-analysis-with-python-
2020\\part02-e04_word_frequencies\\src/alice.txt

Resources