This question already has answers here:
Parsing a mix of Backward slash and forward slash in a filename
(3 answers)
Closed 5 years ago.
When I am trying to send a .exe file using send_file() function of Flask (python 2.7) I get this error
IOError: [Errno 22] invalid mode ('rb') or filename:
'C:\\Users\\Dell\\Desktop\\mom\x08uild\\s6\\s6.exe'
where s6.exe is my file I want to send
The \x08 in mom\x08uild is a backspace. That's probably not what you intended: backspaces are not valid in normal Windows filenames, hence the 'invalid filename' error.
It's a bit hard to know for sure without seeing the code, but you might have forgotten to escape a backslash somewhere, causing a \b to appear (which also means backspace).
Related
This question already has an answer here:
How to correctly pass subprocess arguments
(1 answer)
Closed 1 year ago.
I am trying to rename a drives volume label but i get an error if the label contains a space
import subprocess
subprocess.run(['label', 'L:test 1'])
Produces this Error:
Access Denied as you do not have sufficient privileges or
the disk may be locked by another process.
You have to invoke this utility running in elevated mode
and make sure the disk is unlocked.
The code works fine if I remove the space subprocess.run(['label', 'L:test1'])
How can I add spaces to my label?
try subprocess.run(['label', 'L:test', '1']).
It works via concatenation - silly, but true.
This question already has answers here:
FileNotFoundError: [Errno 2] No such file or directory [duplicate]
(6 answers)
Closed 1 year ago.
I am having a small issue that I cannot seem to solve. My program requires the user to input a path to a .csv file and then the program does stuff with it. Here is my code:
import pandas as pd
path = input("Please enter a path to a .csv file")
data = pd.read_csv(path)
I am running it in my terminal, so dragging the file into it yields what I believe to be the absolute path. The path looks like /Users/me/Downloads/sample.csv and the error message is FileNotFoundError: [Errno 2] File b'/Users/me/Downloads/sample.csv ' does not exist: b'/Users/me/Downloads/sample.csv '
I attempted to concatenate an r in front of it so that it would treat it as a raw string (that's what my google search yielded) but that just put r's in the path. So my question is where are these b's coming from before the path and how do I make this variable path work?
To answer your question: the b prefix indicates that your path value is a byte literal, not a string.
Taking a look at the available Pandas documentation, it seems like read_csv expects a string value for the path. Although it may accept a byte literal value, it may not be able to handle it in an expected way.
This answer provides a good distinction between strings (sequences of characters) and byte literals (sequences of bytes), and this one demonstrates one way you could decode your byte-literal into a string value
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 15 days ago.
The requirement of code is I have to take the file path from the user in the console and perform some action on that file. Users can give paths in windows style or mac style. for the mac or Linux, the path code is working fine but for the windows path its give an error (because of ) how to handle this error as I can't use the 'r' string in that as it's coming from the user.
user_path = input('give text file path: ')
file = open(user_path, 'r')
words = file.read().split()
print('total number of words: ', len(words))
And if I provide path: C:\desktop\file.txt
its give error
Use C:\\desktop\\file.txt instead of C:\desktop\file.txt.
The error is due to the fact that python is recognizing '\f' in 'C:\desktop\file.txt' as the form feed escape sequence.
It can simply be resolved by using forward slash '/' instead of backslash while providing input.
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 2 years ago.
I have isolated a problematic part of my code and run it as 3 lines (see below), yet I still get this weird error where python tells me I used invalid argument, which looks different than the argument passed in. It apparently at random replaces one of my backslashes by double back slash (which shouldn't matter) and alters the name of the file i want opened.
Code:
fl = open("D:\test\sysi\temporary\fe_in.txt","rt")
flstr = fl.read()
ff = flstr.split("---")[1]
Error:
OSError: [Errno 22] Invalid argument: 'D:\test\\sysi\temporary\x0ce_in.txt'
Anybody has encountered this? Any ideas what could be causing this or what could I try? (I have already deleted and re-created the file in question thinking it could be corrupted, didn't change anything)
I tested it and I needed to use double slash "\\" to use this without your error:
fl = open("D:\\test\\sysi\\temporary\\fe_in.txt","rt")
flstr = fl.read()
ff = flstr.split("---")[1]
I believe it is because a single slash will be used as an escape character.
We do not know if your file is corrupted as you suspected in you question since the argument to "open" already was invalid. Your textfile was never opened before the error.
Now I only get, "[Errno 2] No such file or directory: 'D:\test\sysi\temporary\fe_in.txt'" because I did not place a file like yours in my file system, but if you have it the program should succeed.
try:
open(r"D:\test\sysi/temporary\fe_in.txt","r")
or
open(r"D:/test/sysi/temporary/fe_in.txt","r")
or
path = r"D:\test\sysi\temporary\fe_in.txt"
surely someone will work
This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 5 years ago.
I'm somewhat new to Python, but I have enough under my belt to know what I'm doing. What I'm trying to do is write a few lines for a .txt file (as well as a variable), and then print 5 of those characters.
import os
username = "Chad_Wigglybutt"
file = open("testfile.txt", "w")
file.write("Hello .txt file, ")
file.write("This is a test, ")
file.write("Can this write variables? ")
file.write("Lets see: ")
file.write(username)
file.close()
It then creates the file without issue, but when I add
print file.read(5)
to the code, it gives me a syntax error for file.read, and I have no clue why. I've been on the internet for a few hours now and I can't find anything. Either I'm extremely bad at google searching and I'm an idiot, or something's broken, or both. Any tips/ideas? :/
You're writing Python 3 code. In Python 3, print is a function, not a special statement. You need parentheses for function calls:
print(file.read(5))