0

Been having trouble with using variables in a new dirpath when using with open to create a csv file. I can create the dirpath fine along with the .csv filename and add them with os.path.join . But when i try to use the entire correctly formatted path in the open statement it is adding an extra backslash next to the current backslashes in the string?? could not find any documentation on why this may be happening.

This is the error

FileNotFoundError: [Errno 2] No such file or directory: 'Logs\\14-05-2018\\dfg.csv'

If i simply write the following i can create the subfolders and csvs no problemo.

with open(Logs\1234\asd.csv) 'w') as csvfile:

So this is what i am getting an error in on line 43 which is the with open(csvlogPath, 'w') as csvfile: I have tried with r instead of 'w' and same problem. At a loss after two days time to ask you guys. :)

import csv, os.path

#Get and create csv filename string
logFN = input("Please name your trade.\n#")            
suffix = '.csv'
csvname = logFN + suffix
#Create dir name string
t = time.strftime("%d-%m-%Y-%H%M")
dirsname = os.path.join('Logs', t)

csvlogPath = os.path.join(dirsname,csvname)

with open(csvlogPath, 'w') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',', lineterminator='\n', quotechar='|', quoting=csv.QUOTE_MINIMAL)

    filewriter.writerow(['get', 'new', 'values', 'later'])

with open(csvlogPath, 'w') as f:
    reader = csv.reader(f)
 # read file row by row
    rwsCount = 0    
    for row in reader:        
        print (row, rwsCount)
        rwsCount += 1

line_number = rwsCount-1
print (line_number)

with open(csvlogPath, 'w') as f:
    mycsv = csv.reader(f)
    mycsv = list(mycsv)
    text = mycsv[line_number][0]

print (text) 

2 Answers 2

1

you need to make the dirs in python you can use os.makedirs(path) like so:

csvlogPath = os.path.join(dirsname,csvname)
os.makedirs(os.path.dirname(csvlogPath), exist_ok=True)
with open(csvlogPath, 'w') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',', lineterminator='\n', quotechar='|', quoting=csv.QUOTE_MINIMAL)

    filewriter.writerow(['get', 'new', 'values', 'later'])

With open, use 'r' to read and 'w' to write so you need to change the next to opens to:

with open(csvlogPath, 'r') as f:
    reader = csv.reader(f)
 # read file row by row
    rwsCount = 0    
    for row in reader:        
        print (row, rwsCount)
        rwsCount += 1

line_number = rwsCount-1
print (line_number)

with open(csvlogPath, 'r') as f:
    mycsv = csv.reader(f)
    mycsv = list(mycsv)
    text = mycsv[line_number][0]
Sign up to request clarification or add additional context in comments.

Comments

1

test this code :

import csv, os.path

#Get and create csv filename string
import time

logFN = input("Please name your trade.\n#")
suffix = '.csv'
csvname = logFN + suffix
#Create dir name string
t = time.strftime("%d-%m-%Y-%H%M")
dirsname = os.path.join('Logs', t)

###########################
if not os.path.exists(dirsname):
    os.makedirs(dirsname)
###########################


csvlogPath = os.path.join(dirsname,csvname)

with open(csvlogPath, 'w') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',', lineterminator='\n', quotechar='|', quoting=csv.QUOTE_MINIMAL)

    filewriter.writerow(['get', 'new', 'values', 'later'])

with open(csvlogPath, 'r') as f:
    reader = csv.reader(f)
 # read file row by row
    rwsCount = 0
    for row in reader:
        print (row, rwsCount)
        rwsCount += 1

line_number = rwsCount-1
print (line_number)

with open(csvlogPath, 'r') as f:
    mycsv = csv.reader(f)
    mycsv = list(mycsv)
    text = mycsv[line_number][0]

print (text)

1 Comment

also perfect!! but has the checking if already created so a bit better, i did actually try a version of this but failed earlier

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.