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

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

Related

How to enter windows paths to run Python in Sublime Text 3 using Ctrl+B

I'm trying to run a python script using Sublime Text 3. I'm trying to just use Ctrl+b and type the parameters in the box that comes up, but I can't for the life of me figure out how to format the Windows file paths. I keep getting a FileNotFoundError
I've tried:
"C:\dir1\dir 2\file.ext"
"C:\\dir1\\dir 2\\file.ext"
"C:/dir1/dir 2/file.ext"
Because some of my directories have spaces in them, I'm enclosing the whole thing in double quotes no matter which slash style I try. What am I missing here? None of these works.
With the first, for example, I'm getting FileNotFoundError: [Errno 2] No such file or directory: 'C:\\dir1\\dir 2\\file.ext,' [Finished in 0.3s]
The file is most definitely there and spelled correctly.
In case it matters, I'm using docopt to parse the input parameters

How to save python file in jupyter note if path contains spaces %20

When I try to save a file in a jupyter notebook I get an error saying there is no file or directory.
Error Message ___________________________________________________________
Unexpected error while saving file: OneDrive%%20-%%20Files/Desktop/PythonProjects/bhp.ipynb [Errno 2] No such file or directory: 'C:\Users\username\OneDrive%%20-%%20Files\Desktop\PythonProjects\bhp.ipynb'
I began having this problem after a software update where now I can only save things to "OneDrive - Files". Unfortunately, the path to OneDrive contains several spaces - causing this error every time I try to save a new file. I would use a different directory if I could. I was hoping for an easy fix like wrapping the path in quotes - but so far this didn't work.
Replacing the %20 characters with a literal space resolved the issue. I suppose the Jupyter Notebook treats the path the same way as a url and inserts %20 characters for spaces. But when saving to a directory on a pc - the %20 characters can be replaced with a space - without the need to wrap the path in quotations or to place backslashes before the spaces.

Python: "FileNotFoundError" Despite being able to print such files

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?)

"\n" in python messes with chmod

I am making a program where I need permission to file paths so I am using the chmod command. I get the paths from a text document. I put the paths there and organized them by rows by using the "/n" command. The problem is when I plug the variable in the string still has a /n on it. I was wondering if there was a way to have a txt document go down a line but in a way it wont interfere.
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:/Users/*****/Pictures/Camera Roll\n'
I have python 3.6.2 and windows 10. I am still in the early stages of learning this language.
You want to .strip() the path before sending it to chmod:
st = 'abcd\n'
st.strip() == 'abcd' # strip() trims all whitespace

An error when loading a 2mb dataset of floating points (python)

Does any one know why i got an error of "FileNotFoundError: [Errno 2] No such file or directory: 'bcs.xlsx'" when i'm loading this file of size 2mb it has around 60,000 rows and 4 columns.
i tried using csv instead of xlsx but i get the same error and i've checked hundreds times that the script and the file are at he same directory.
This is because Python does not find your file, errors are not lying.
But there's a misunderstanding in your question, you checked that the file is in the same directory as your script, but that's not the check you have to do. You have to check the file is in the current working directory of your python script.
To see your current working directory, use:
import os
print(os.getcwd())
And as we're at it you can list this directory:
print(os.listdir())
I don't know how you execute your script, but if you're using a terminal emulator, a typical way to give a file name to a program is by argument, not hardcoding its name, like by using argparse. And if you do this way, your shell completion may help you naming your file properly, like:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()
print(args.file.read())
Now on a shell if you type:
python3 ./thescript.py ./th[TAB]
your shell will autocomplete "./th" to "./thescript.py" (if and only if it exists), highly reducing the probablity of having a typo. Typically if there's a space in the filename like "the script.py", your shell should properly autocomplete the\ script.py.
Also if you use argparse with the argparse.FileType as I did, you'll have a verbose error in case the file does not exist:
thescript.py: error: argument file: can't open 'foo': [Errno 2] No such file or directory: 'foo'
But… you already have a verbose error.

Resources