I'm trying to make the migration from Windows to Linux, some of my Python code has not survived the move.
this is the Windows version:
path = r'C:\\Users\\x\\PythonTestFiles\\TestFile.csv'
file = open(path, newline='')
TestFileRaw = csv.reader(file)
header = next(TestFileRaw)
dataPC = [row for row in TestFileRaw]
etc....
I'm trying to address a csv file in a certain directory but I can't figure out the Linux addressing.
Can anyone Help?
here is some info for linux directory structure
press Alt + Enter over your file then in that properties dialog copy the path to root its something like /home/<user_name>/Documents then append that path with file name of csv file.
At Final your csv file's path becomes something like
/home/<user_name>/Documents/filename.csv
You could use separator from os module:
import os
path = os.path.join('your_folder_name', 'your_file_name')
That way it could be Windows/Linux independent.
Here is official documentation to os.path module.
Use whatever path is your csv file in linux!
path = r'/home/x/python_test_files/test_file.csv'
file = open(path, newline='')
TestFileRaw = csv.reader(file)
header = next(TestFileRaw)
dataPC = [row for row in TestFileRaw]
Related
I'm looking to retrieve a list of CSV files, and use these names as variables to open and retrieve their content. Something like this:
import csv
import os
files = os.listdir('C:/csvs')
with open(files[0], 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
if line[1]=="**STAFF**":
pass
else:
print(line)
If I print files[0], I do get the correct content, but when I try the above code it does not work.
os.listdir(directory_path) gives filenames which are inside the folder. To actually use the file you need the full path (absolute or relative). This can be easily done by appending each file's name to the directory_path like this:
import os
files = os.listdir(directory_path)
full_file_path = os.path.join(directory_path, files[0])
You can also use glob to save the trouble of joining the paths.
If I want to read files named "ABCbook.txt" under directors A/B/C, which means the path A/B/C depends on the files' name and the folders A/B/C are hierarchical. How can I achieve it in Python?
We will use os.path.join to make the file path in a platform-independent way, then open the file using the normal with open... technique.
import os
my_file = os.path.join('A', 'B', 'C', 'ABCbook.txt')
with open(my_file) as f:
# your code to work on the file goes here
for line in f:
print(line)
import os
filename = "ABCbook.txt"
path = list(filename[:3]) + [filename]
syspath = os.path.join(*path)
print(syspath)
output (on windows):
A\B\C\ABCbook.txt
on linux or mac it will return
A/B/C/ABCbook.txt
I have a csv file that looks like this:
dc_identifier,aubrey_identifier
AR0776-280206-LT513-01,metadc1084267
AR0776-280206-LT513-02,metadc1083385
AR0776-280206-LT513-03,metadc1084185
AR0776-280206-LT513-04,metadc1083449
AR0776-280206-LT513-05,metadc1084294
AR0776-280206-LT513-06,metadc1083393
AR0776-280206-LT513-07,metadc1083604
AR0776-280206-LT513-08,metadc1083956
AR0776-280206-LT513-09,metadc1083223
AR0776-280206-LT513-10,metadc1084224
I need to create folders with the "metadc#######" names within the directory that the script will live in.
Here's what I have so far:
import os
import fileinput
path = 'C:\Users\gpp0020\Desktop\TestDir'
textFile = 'C:\Users\gpp0020\Desktop\TestDir\kxas_ids.csv'
myList = open(textFile, 'rb+')
for line in myList:
for item in line.strip().split(','):
os.makedirs(os.path.join(path, item))
print 'created', item
However! I also need the program to grab files that are named with the identifiers (AR0776-280206-LT513-01, etc) and put them in the corresponding metadc number, according to the csv. Each file is doubled (one .mkv file, and one .mkv.md5 checksum file) and both need to go into the folder.
What's the best way to go about this?
Use the csv library to help with reading the file in:
import csv
import os
import shutil
path = r'C:\Users\gpp0020\Desktop\TestDir'
with open('kxas_ids.csv', 'r', newline='') as f_input:
csv_input = csv.reader(f_input)
header = next(csv_input)
for dv, aubrey in csv_input:
os.makedirs(os.path.join(path, aubrey), exist_ok=True)
mkv = '{}.mkv'.format(dv)
shutil.copy2(os.path.join(path, mkv), os.path.join(path, aubrey, mkv))
mkv_md5 = '{}.mkv.md5'.format(dv)
shutil.copy2(os.path.join(path, mkv_md5), os.path.join(path, aubrey, mkv_md5))
This would for example:
Create a folder called C:\Users\gpp0020\Desktop\TestDir\metadc108426
Copy a file called AR0776-280206-LT513-01.mkv into it.
Copy a file called AR0776-280206-LT513-01.mkv.md5 into it.
It assumes that all files are found in path
I use setuptools to distribute a python package. My directory structure is like following.
Mypackage
--setup.py
--my_package.py
Data
--some_name.log
I want users to put data files in this folder, and name can be anything with the extension .log.
log_list = []
for file in glob.glob('/home/ginger/Mypackage/Data/*.log'):
with open(file,'r') as f:
for line in f:
try:
data = p.parse(line)
except:
pass
log_list.append(data)
This code works fine. But when I try to get the absolute path from the relative path it does not read the data file. Why is this?
path = os.path.abspath("Data/*.log")
log = []
for file in glob.glob(path):
with open(file,'r') as f:
for line in f:
log.append(line)
Found the solution. Path has to be defined as following with the immediate directory.
path = os.path.abspath("Mypackage/Data/*.log")
I'm trying to take the input file and save it into a new folder on my computer, but I can't figure out how to do it correctly.
Here is the code I tried:
from os.path import join as pjoin
a = raw_input("File Name: ")
filepath = "C:\Documents and Settings\User\My Documents\'a'"
fout = open(filepath, "w")
path_to_file = pjoin("C:\Documents and Settings User\My Documents\Dropbox",'a')
FILE = open(path_to_file, "w")
When I run it, it's putting two \ in between each sub-directory instead of one and it's telling me it's not an existing file or directory.
I am sure there is an easier way to do this, please help.
Why do you have unescaped "'quotes_like_this_inside_quotes'"? That may be a reason for that failure.
From what I can understand, the directories you are saving to are "C:\Documents and Settings\User\My Documents\' and 'C:\Documents and Settings\User\My Documents\'.
Whenever you are messing with directories/paths ALWAYS use os.expanduser('~/something/blah').
Try this:
from os.path import expanduser, join
path_to_file1 = join(expanduser('~/Dropbox/'), 'a')
path_to_file2 = join(expanduser('~'), 'a')
fout = open(path_to_file2, "w")
FILE = open(path_to_file1, "w")
And the double-backslashes are OK, AFAIK.
Let me know if this works - I'm not on a Windows box at the moment.