Python Forum
Writing the Path to a file properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing the Path to a file properly
#1
Hello,

I am 64 years old but new to Python and brand new to this forum. If I due something wrong please let me know.

I purchased a book on python and I'm working through it. I am trying to work on opening/reading/writing to files. I just cannot open a file in another directory from my python script location. I have followed every direction and googled until my fingers are bleeding. I am obviously missing something.

My computer is Windows 10
My python file is saved in Dropbox (I sometimes study from different locations)
Here is my code:

import csv
import os

path = "This PC/Windows(C:)/Users/dcw99/Documents/Python_basics_work_files/python-basics-exercises-master" \
		"/ch11-file-input-and-output/practice_files/"

with open(os.path.join(path, "wonka.csv"), "r") as my_file:
	reader = csv.reader(my_file)
	for row in reader:
		print(row)

I keep getting the error:
Error:
OSerror: [error22] Invalid Argument:"This PC/Windows(C:)/Users/dcw99/Documents/Python_basics_work_files/python-basics-exercises-master" \ "/ch11-file-input-and-output/practice_files/wonka.csv"
I have typed the path exactly as it appears. I've placed an r immediately in front of the path, as instructed. No joy.

I apologize for asking about something this simple but I am just so frustrated. Any help you can offer would be very appreciated.

Thanks,
Don
Reply
#2
Try the following as the path
path = ("C:/Users/dcw99/Documents/Python_basics_work_files/python-basics-exerci"
        "ses-master/ch11-file-input-and-output/practice_files/")
Reply
#3
Yoriz!!

Worked like a charm!!! Thank you so much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 559 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  File path by adding various variables Mishal0488 2 963 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Script File Failure-Path Error? jerryf 13 3,313 Nov-30-2022, 09:58 AM
Last Post: jerryf
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,406 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,148 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Subprocess.Popen() not working when reading file path from csv file herwin 13 14,614 May-07-2021, 03:26 PM
Last Post: herwin
  Add file to sys.path permanently hcccs 5 8,176 Jan-31-2021, 11:26 AM
Last Post: hcccs
  Writing to file ends incorrectly project_science 4 2,641 Jan-06-2021, 06:39 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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