Pandas cannot read same json files saved in sub-folder
I have 5 json files saved in my current working directory, and the following code works fine for me to read each of them for further analysis:
import pandas as pd
import os
path=os.path.join('.') #Just want to as an example here
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.json')]
for i in files:
df=pd.read_json(i)
allfiles
But when I create a sub-folder in my current working directory, and move my 5 json files to it, then I cnanot read them:
import pandas as pd
import os
path=os.path.join('.','Result')#New Folder:Result
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.json')]
for i in files:
df=pd.read_json(i)
allfiles
The error message is: ValueError: Trailing data
I try to search it via google, but still don't know what is going on, i didn't change anything, just create a sub-folder.
python-3.x pandas
add a comment |
I have 5 json files saved in my current working directory, and the following code works fine for me to read each of them for further analysis:
import pandas as pd
import os
path=os.path.join('.') #Just want to as an example here
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.json')]
for i in files:
df=pd.read_json(i)
allfiles
But when I create a sub-folder in my current working directory, and move my 5 json files to it, then I cnanot read them:
import pandas as pd
import os
path=os.path.join('.','Result')#New Folder:Result
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.json')]
for i in files:
df=pd.read_json(i)
allfiles
The error message is: ValueError: Trailing data
I try to search it via google, but still don't know what is going on, i didn't change anything, just create a sub-folder.
python-3.x pandas
In your code you have not toldpandas
to look inside the sub-directory. The output ofos.listdir(path)
does not contain the path.
– Kapil
Nov 23 '18 at 8:34
my path is '.' right? because os.listdir('.') gives me all files in my current working dirct, thanks
– Rowling
Nov 23 '18 at 8:39
In other words if you have a file./Result/a.json
then you are runningdf=pd.read_json("a.json")
which will not work.
– Kapil
Nov 23 '18 at 8:42
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['a.json']; but if I didn't include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:54
add a comment |
I have 5 json files saved in my current working directory, and the following code works fine for me to read each of them for further analysis:
import pandas as pd
import os
path=os.path.join('.') #Just want to as an example here
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.json')]
for i in files:
df=pd.read_json(i)
allfiles
But when I create a sub-folder in my current working directory, and move my 5 json files to it, then I cnanot read them:
import pandas as pd
import os
path=os.path.join('.','Result')#New Folder:Result
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.json')]
for i in files:
df=pd.read_json(i)
allfiles
The error message is: ValueError: Trailing data
I try to search it via google, but still don't know what is going on, i didn't change anything, just create a sub-folder.
python-3.x pandas
I have 5 json files saved in my current working directory, and the following code works fine for me to read each of them for further analysis:
import pandas as pd
import os
path=os.path.join('.') #Just want to as an example here
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.json')]
for i in files:
df=pd.read_json(i)
allfiles
But when I create a sub-folder in my current working directory, and move my 5 json files to it, then I cnanot read them:
import pandas as pd
import os
path=os.path.join('.','Result')#New Folder:Result
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.json')]
for i in files:
df=pd.read_json(i)
allfiles
The error message is: ValueError: Trailing data
I try to search it via google, but still don't know what is going on, i didn't change anything, just create a sub-folder.
python-3.x pandas
python-3.x pandas
asked Nov 23 '18 at 8:16
Rowling
1589
1589
In your code you have not toldpandas
to look inside the sub-directory. The output ofos.listdir(path)
does not contain the path.
– Kapil
Nov 23 '18 at 8:34
my path is '.' right? because os.listdir('.') gives me all files in my current working dirct, thanks
– Rowling
Nov 23 '18 at 8:39
In other words if you have a file./Result/a.json
then you are runningdf=pd.read_json("a.json")
which will not work.
– Kapil
Nov 23 '18 at 8:42
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['a.json']; but if I didn't include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:54
add a comment |
In your code you have not toldpandas
to look inside the sub-directory. The output ofos.listdir(path)
does not contain the path.
– Kapil
Nov 23 '18 at 8:34
my path is '.' right? because os.listdir('.') gives me all files in my current working dirct, thanks
– Rowling
Nov 23 '18 at 8:39
In other words if you have a file./Result/a.json
then you are runningdf=pd.read_json("a.json")
which will not work.
– Kapil
Nov 23 '18 at 8:42
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['a.json']; but if I didn't include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:54
In your code you have not told
pandas
to look inside the sub-directory. The output of os.listdir(path)
does not contain the path.– Kapil
Nov 23 '18 at 8:34
In your code you have not told
pandas
to look inside the sub-directory. The output of os.listdir(path)
does not contain the path.– Kapil
Nov 23 '18 at 8:34
my path is '.' right? because os.listdir('.') gives me all files in my current working dirct, thanks
– Rowling
Nov 23 '18 at 8:39
my path is '.' right? because os.listdir('.') gives me all files in my current working dirct, thanks
– Rowling
Nov 23 '18 at 8:39
In other words if you have a file
./Result/a.json
then you are running df=pd.read_json("a.json")
which will not work.– Kapil
Nov 23 '18 at 8:42
In other words if you have a file
./Result/a.json
then you are running df=pd.read_json("a.json")
which will not work.– Kapil
Nov 23 '18 at 8:42
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['a.json']; but if I didn't include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:54
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['a.json']; but if I didn't include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:54
add a comment |
3 Answers
3
active
oldest
votes
When opening the file using pandas you are passing only the filename not the absolute path. so when script is running from current directory where file is present it is able to open it.
but when you have moved the files to Result directory it is still searching for file in current directory.
edited below code to have full directory path in filenames array.
cwd = os.getcwd()
path=os.path.join(cwd) #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXall_data.json']
glossary
GlossDiv {'title': 'S', 'GlossList': {'GlossEntry':
{'I...
path=os.path.join(cwd,'Result') #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXResultall_data1.json']
glossary GlossDiv {'title': 'S', 'GlossList': {'GlossEntry': {'I...
sample data in file :
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
&Rishi Bansal mentioned I can get the cwd via os.getcwd(), and create my path via: os.path.join(wd,'Result'); i think it is looks good, but it still gives me error
– Rowling
Nov 23 '18 at 8:49
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['json_test.json']; but if I didn;t include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:53
@Frank I have edited my answer to include cwd, also tried on sample json it is working for me, what error you are getting now?
– Sach
Nov 23 '18 at 9:17
I pass the: files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')], and this is working
– Rowling
Nov 23 '18 at 9:33
ok great, I think you have included the cwd part also.
– Sach
Nov 23 '18 at 9:39
|
show 1 more comment
Try Current working directory and then join the new sub directory.
cwd = os.getcwd()
path=os.path.join(cwd,'Result')#New Folder:Result
I just tested the code on .pdf as I am having pdf in Downloads directory.
import pandas as pd
import os
cwd = os.getcwd()
path=os.path.join(cwd,"Downloads")#New Folder:Result
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.pdf')]
for i in files:
print i
It worked for me.
my path is '.' right? os.listdir('.') gives your all files in my current working dirct
– Rowling
Nov 23 '18 at 8:39
but when you try to join "." with other string not sure how it will behave.
– Rishi Bansal
Nov 23 '18 at 9:06
it didn't work...
– Rowling
Nov 23 '18 at 9:12
add a comment |
You could use glob.glob
, which returns a list of paths that match your pathname
:
import glob
pathname = 'Result/*.json'
list_of_paths_to_files = glob.glob(pathname)
If you want to read the files as many separate DataFrame
:
list_of_dataframes = [pd.read_json(file_path) for file_path in list_of_paths_to_files]
If you want one DataFrame
:
df = pd.concat(list_of_dataframes)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442878%2fpandas-cannot-read-same-json-files-saved-in-sub-folder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
When opening the file using pandas you are passing only the filename not the absolute path. so when script is running from current directory where file is present it is able to open it.
but when you have moved the files to Result directory it is still searching for file in current directory.
edited below code to have full directory path in filenames array.
cwd = os.getcwd()
path=os.path.join(cwd) #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXall_data.json']
glossary
GlossDiv {'title': 'S', 'GlossList': {'GlossEntry':
{'I...
path=os.path.join(cwd,'Result') #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXResultall_data1.json']
glossary GlossDiv {'title': 'S', 'GlossList': {'GlossEntry': {'I...
sample data in file :
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
&Rishi Bansal mentioned I can get the cwd via os.getcwd(), and create my path via: os.path.join(wd,'Result'); i think it is looks good, but it still gives me error
– Rowling
Nov 23 '18 at 8:49
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['json_test.json']; but if I didn;t include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:53
@Frank I have edited my answer to include cwd, also tried on sample json it is working for me, what error you are getting now?
– Sach
Nov 23 '18 at 9:17
I pass the: files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')], and this is working
– Rowling
Nov 23 '18 at 9:33
ok great, I think you have included the cwd part also.
– Sach
Nov 23 '18 at 9:39
|
show 1 more comment
When opening the file using pandas you are passing only the filename not the absolute path. so when script is running from current directory where file is present it is able to open it.
but when you have moved the files to Result directory it is still searching for file in current directory.
edited below code to have full directory path in filenames array.
cwd = os.getcwd()
path=os.path.join(cwd) #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXall_data.json']
glossary
GlossDiv {'title': 'S', 'GlossList': {'GlossEntry':
{'I...
path=os.path.join(cwd,'Result') #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXResultall_data1.json']
glossary GlossDiv {'title': 'S', 'GlossList': {'GlossEntry': {'I...
sample data in file :
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
&Rishi Bansal mentioned I can get the cwd via os.getcwd(), and create my path via: os.path.join(wd,'Result'); i think it is looks good, but it still gives me error
– Rowling
Nov 23 '18 at 8:49
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['json_test.json']; but if I didn;t include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:53
@Frank I have edited my answer to include cwd, also tried on sample json it is working for me, what error you are getting now?
– Sach
Nov 23 '18 at 9:17
I pass the: files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')], and this is working
– Rowling
Nov 23 '18 at 9:33
ok great, I think you have included the cwd part also.
– Sach
Nov 23 '18 at 9:39
|
show 1 more comment
When opening the file using pandas you are passing only the filename not the absolute path. so when script is running from current directory where file is present it is able to open it.
but when you have moved the files to Result directory it is still searching for file in current directory.
edited below code to have full directory path in filenames array.
cwd = os.getcwd()
path=os.path.join(cwd) #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXall_data.json']
glossary
GlossDiv {'title': 'S', 'GlossList': {'GlossEntry':
{'I...
path=os.path.join(cwd,'Result') #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXResultall_data1.json']
glossary GlossDiv {'title': 'S', 'GlossList': {'GlossEntry': {'I...
sample data in file :
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
When opening the file using pandas you are passing only the filename not the absolute path. so when script is running from current directory where file is present it is able to open it.
but when you have moved the files to Result directory it is still searching for file in current directory.
edited below code to have full directory path in filenames array.
cwd = os.getcwd()
path=os.path.join(cwd) #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXall_data.json']
glossary
GlossDiv {'title': 'S', 'GlossList': {'GlossEntry':
{'I...
path=os.path.join(cwd,'Result') #Just want to as an example here
allfiles = os.listdir(path)
files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')]
print(files)
for f in files :
df1 = pd.read_json(f)
print(df1.head(1))
['C:UsersXXXXXXXXXResultall_data1.json']
glossary GlossDiv {'title': 'S', 'GlossList': {'GlossEntry': {'I...
sample data in file :
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
edited Nov 23 '18 at 9:38
answered Nov 23 '18 at 8:44
Sach
628417
628417
&Rishi Bansal mentioned I can get the cwd via os.getcwd(), and create my path via: os.path.join(wd,'Result'); i think it is looks good, but it still gives me error
– Rowling
Nov 23 '18 at 8:49
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['json_test.json']; but if I didn;t include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:53
@Frank I have edited my answer to include cwd, also tried on sample json it is working for me, what error you are getting now?
– Sach
Nov 23 '18 at 9:17
I pass the: files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')], and this is working
– Rowling
Nov 23 '18 at 9:33
ok great, I think you have included the cwd part also.
– Sach
Nov 23 '18 at 9:39
|
show 1 more comment
&Rishi Bansal mentioned I can get the cwd via os.getcwd(), and create my path via: os.path.join(wd,'Result'); i think it is looks good, but it still gives me error
– Rowling
Nov 23 '18 at 8:49
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['json_test.json']; but if I didn;t include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:53
@Frank I have edited my answer to include cwd, also tried on sample json it is working for me, what error you are getting now?
– Sach
Nov 23 '18 at 9:17
I pass the: files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')], and this is working
– Rowling
Nov 23 '18 at 9:33
ok great, I think you have included the cwd part also.
– Sach
Nov 23 '18 at 9:39
&Rishi Bansal mentioned I can get the cwd via os.getcwd(), and create my path via: os.path.join(wd,'Result'); i think it is looks good, but it still gives me error
– Rowling
Nov 23 '18 at 8:49
&Rishi Bansal mentioned I can get the cwd via os.getcwd(), and create my path via: os.path.join(wd,'Result'); i think it is looks good, but it still gives me error
– Rowling
Nov 23 '18 at 8:49
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['json_test.json']; but if I didn;t include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:53
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['json_test.json']; but if I didn;t include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:53
@Frank I have edited my answer to include cwd, also tried on sample json it is working for me, what error you are getting now?
– Sach
Nov 23 '18 at 9:17
@Frank I have edited my answer to include cwd, also tried on sample json it is working for me, what error you are getting now?
– Sach
Nov 23 '18 at 9:17
I pass the: files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')], and this is working
– Rowling
Nov 23 '18 at 9:33
I pass the: files = [os.path.join(path,files) for files in allfiles if files.endswith('.json')], and this is working
– Rowling
Nov 23 '18 at 9:33
ok great, I think you have included the cwd part also.
– Sach
Nov 23 '18 at 9:39
ok great, I think you have included the cwd part also.
– Sach
Nov 23 '18 at 9:39
|
show 1 more comment
Try Current working directory and then join the new sub directory.
cwd = os.getcwd()
path=os.path.join(cwd,'Result')#New Folder:Result
I just tested the code on .pdf as I am having pdf in Downloads directory.
import pandas as pd
import os
cwd = os.getcwd()
path=os.path.join(cwd,"Downloads")#New Folder:Result
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.pdf')]
for i in files:
print i
It worked for me.
my path is '.' right? os.listdir('.') gives your all files in my current working dirct
– Rowling
Nov 23 '18 at 8:39
but when you try to join "." with other string not sure how it will behave.
– Rishi Bansal
Nov 23 '18 at 9:06
it didn't work...
– Rowling
Nov 23 '18 at 9:12
add a comment |
Try Current working directory and then join the new sub directory.
cwd = os.getcwd()
path=os.path.join(cwd,'Result')#New Folder:Result
I just tested the code on .pdf as I am having pdf in Downloads directory.
import pandas as pd
import os
cwd = os.getcwd()
path=os.path.join(cwd,"Downloads")#New Folder:Result
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.pdf')]
for i in files:
print i
It worked for me.
my path is '.' right? os.listdir('.') gives your all files in my current working dirct
– Rowling
Nov 23 '18 at 8:39
but when you try to join "." with other string not sure how it will behave.
– Rishi Bansal
Nov 23 '18 at 9:06
it didn't work...
– Rowling
Nov 23 '18 at 9:12
add a comment |
Try Current working directory and then join the new sub directory.
cwd = os.getcwd()
path=os.path.join(cwd,'Result')#New Folder:Result
I just tested the code on .pdf as I am having pdf in Downloads directory.
import pandas as pd
import os
cwd = os.getcwd()
path=os.path.join(cwd,"Downloads")#New Folder:Result
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.pdf')]
for i in files:
print i
It worked for me.
Try Current working directory and then join the new sub directory.
cwd = os.getcwd()
path=os.path.join(cwd,'Result')#New Folder:Result
I just tested the code on .pdf as I am having pdf in Downloads directory.
import pandas as pd
import os
cwd = os.getcwd()
path=os.path.join(cwd,"Downloads")#New Folder:Result
allfiles = os.listdir(path)
files = [files for files in allfiles if files.endswith('.pdf')]
for i in files:
print i
It worked for me.
edited Nov 23 '18 at 9:23
answered Nov 23 '18 at 8:35
Rishi Bansal
588217
588217
my path is '.' right? os.listdir('.') gives your all files in my current working dirct
– Rowling
Nov 23 '18 at 8:39
but when you try to join "." with other string not sure how it will behave.
– Rishi Bansal
Nov 23 '18 at 9:06
it didn't work...
– Rowling
Nov 23 '18 at 9:12
add a comment |
my path is '.' right? os.listdir('.') gives your all files in my current working dirct
– Rowling
Nov 23 '18 at 8:39
but when you try to join "." with other string not sure how it will behave.
– Rishi Bansal
Nov 23 '18 at 9:06
it didn't work...
– Rowling
Nov 23 '18 at 9:12
my path is '.' right? os.listdir('.') gives your all files in my current working dirct
– Rowling
Nov 23 '18 at 8:39
my path is '.' right? os.listdir('.') gives your all files in my current working dirct
– Rowling
Nov 23 '18 at 8:39
but when you try to join "." with other string not sure how it will behave.
– Rishi Bansal
Nov 23 '18 at 9:06
but when you try to join "." with other string not sure how it will behave.
– Rishi Bansal
Nov 23 '18 at 9:06
it didn't work...
– Rowling
Nov 23 '18 at 9:12
it didn't work...
– Rowling
Nov 23 '18 at 9:12
add a comment |
You could use glob.glob
, which returns a list of paths that match your pathname
:
import glob
pathname = 'Result/*.json'
list_of_paths_to_files = glob.glob(pathname)
If you want to read the files as many separate DataFrame
:
list_of_dataframes = [pd.read_json(file_path) for file_path in list_of_paths_to_files]
If you want one DataFrame
:
df = pd.concat(list_of_dataframes)
add a comment |
You could use glob.glob
, which returns a list of paths that match your pathname
:
import glob
pathname = 'Result/*.json'
list_of_paths_to_files = glob.glob(pathname)
If you want to read the files as many separate DataFrame
:
list_of_dataframes = [pd.read_json(file_path) for file_path in list_of_paths_to_files]
If you want one DataFrame
:
df = pd.concat(list_of_dataframes)
add a comment |
You could use glob.glob
, which returns a list of paths that match your pathname
:
import glob
pathname = 'Result/*.json'
list_of_paths_to_files = glob.glob(pathname)
If you want to read the files as many separate DataFrame
:
list_of_dataframes = [pd.read_json(file_path) for file_path in list_of_paths_to_files]
If you want one DataFrame
:
df = pd.concat(list_of_dataframes)
You could use glob.glob
, which returns a list of paths that match your pathname
:
import glob
pathname = 'Result/*.json'
list_of_paths_to_files = glob.glob(pathname)
If you want to read the files as many separate DataFrame
:
list_of_dataframes = [pd.read_json(file_path) for file_path in list_of_paths_to_files]
If you want one DataFrame
:
df = pd.concat(list_of_dataframes)
edited Nov 23 '18 at 9:07
answered Nov 23 '18 at 8:49
user3471881
1,0842619
1,0842619
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442878%2fpandas-cannot-read-same-json-files-saved-in-sub-folder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In your code you have not told
pandas
to look inside the sub-directory. The output ofos.listdir(path)
does not contain the path.– Kapil
Nov 23 '18 at 8:34
my path is '.' right? because os.listdir('.') gives me all files in my current working dirct, thanks
– Rowling
Nov 23 '18 at 8:39
In other words if you have a file
./Result/a.json
then you are runningdf=pd.read_json("a.json")
which will not work.– Kapil
Nov 23 '18 at 8:42
I understand no matter what I did above, [files for files in allfiles if files.endswith('.json')] gives me for example a list of file name: ['a.json']; but if I didn't include the path, we I call read_json, it will search for this file in my CWD, but not the result folder
– Rowling
Nov 23 '18 at 8:54