Python Forum
string to file parameter oddity
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string to file parameter oddity
#1
python 2.7
f=r"C:\temp\test.txt"
print f
filein=open(f)
a=filein.read()
filein.close()
print a
it prints
Output:
C:\temp\test.txt
but then it returns ...
Error:
No such file or directory 'C:\\temp\\test.txt'
why is it adding extra \ ?
A string to file parameter oddity that I can't find explained or mentioned anywhere on the net.

help plz, thks
Reply
#2
That has nothing to do with files. Backslash (ie: \) is an escape character. If you want to actually use one in a string, you need to escape it: \\. The only reason it's working for you, is because you marked the string as raw (that's what the "r" in front of your path means). You could also use a single forward slash... python will do The Right Thing depending on what os you're on, so "c:/temp/test.txt" == "c:\\temp\\test.txt" == r"c:\temp\test.txt" == "c:/temp\\test.txt" == r"c:/temp\test.txt".
...but don't mix between the two in a single string. Other people will hate you for purposefully making things hard to read, lol.
Reply
#3
Works for me.
Output:
C:\temp\test.txt this is a test file
You could also write the line
f=r"C:\temp\test.txt"
as
f="C:/temp/test.txt"
Do you have a file named "test.txt" in "C:\temp" ?

In the future, please place code between the code (python) tags and the full traceback between the error tags.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
Yes I have a C:\temp\test.txt file

I tried swapping the backslash for a forward slash, get same error.

Also I tried changing

filein=open(f)
to

filein=open(r"C:\TEMP\test.txt")
gives same error

then tried

filein=open("C:\\TEMP\\test.txt")
gives same error

no such file or directory : ' C:\\TEMP\\test.txt"

Don't think my code is wrong, but some setting might be wrong.
Reply
#5
Then try it with a file in the same directory as your script:
f = open("test.txt")

Or try:
f = open("./test.txt")
f = open("c:/test.txt")
f = open("c:/temp/test.txt")
Reply
#6
Fixed

my file was called test.txt.txt, changed it to test.txt

windows doesn't make such errors too clear.

and the python error message with the double backslashes was a red herring. It shouldn't be doing that if it can't find a file!

thks for help.
Reply
#7
In Windows Explorer it is better to switch off the option "Hide extensions for known file types".
Reply
#8
Like I was saying, the error message never mentioned double backslashes. The python error was always telling you the file didn't exist.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to replace a string with a file (HTML file) tester_V 1 774 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 11,079 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  [SOLVED] Input parameter: Single file or glob? Winfried 0 1,588 Sep-10-2021, 11:54 AM
Last Post: Winfried
  How to fill parameter with data from multiple columns CSV file greenpine 1 1,669 Dec-21-2020, 06:50 PM
Last Post: Larz60+
  I need my compiled Python Mac app to accept a file as a parameter Oethen 2 2,431 May-10-2020, 05:57 PM
Last Post: Oethen
  Python Parameter inside Json file treated as String dhiliptcs 0 1,849 Dec-10-2019, 07:28 PM
Last Post: dhiliptcs
  TreeTagger : parameter file invalid : english.par Raph0909 0 2,804 Apr-12-2019, 12:12 PM
Last Post: Raph0909
  Help with python code to search string in one file & replace with line in other file mforthman 26 11,949 Dec-19-2017, 07:11 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020